- Home /
SqliteException: Unable to open the database file - Ready-made DB Provided To Access
This kind of error, I was getting in my Logcat of Android Studio:
For a similar exception, I have mostly read all posts from Google but I can't able to find a solution that can work for me. So that I have decided to post a new question over here.
I was working on working on a small application using the Unity game engine that I target to publish on Android and iOS platforms.
For this, I require to create a database using SQLite. For the database, I will add all the records within different tables that I need within the application. So mostly I will execute retrieve and update records related queries as per my app need.
Within my iMac, I have done all the basic setup, and its working properly but when I run the game build within the Android device, I started getting the above exception. So nothing gets executed after that.
I have placed my database within the Assets folder of the project. Through the following code, I was trying to access it:
public class SqliteHelper
{
private const string TAG = "Dysphasia: SqliteHelper:\t";
private const string DATABASE_NAME = "DysphasiaDB.db";
public string db_connection_string;
public IDbConnection db_connection;
public SqliteHelper()
{
db_connection_string = "URI=file:" + Application.dataPath + "/" + DATABASE_NAME;
Debug.Log("db_connection_string" + db_connection_string);
db_connection = new SqliteConnection(db_connection_string);
db_connection.Open();
}
~SqliteHelper()
{
db_connection.Close();
}
}
As I mentioned earlier, within Unity editor all things working properly but in Android device its not.
I want to publish my game on Android and iOS both platforms so give me some solution that can work on both the platforms. One important point, I don't want to create a database runtime, I want to provide data to the application ready-made.
I was following this tutorial from first: SQLite and Unity: How to do it right
But in this tutorial, he was creating a database at runtime rather than providing a pre-made database to use.
Hello, i have never used the SqliteConnection class, but almost sure you cant simply pass him the stream assets path
the content inside the strea$$anonymous$$g assets folder gets copied inside the apk, and you need to use the WWW or the UnityWebRequest classes to get the DB and use the downloaded content as DB
do you mean by the default database file not present within the strea$$anonymous$$g assets folder? when we install the game build. We require to manually copy that file over the strea$$anonymous$$g assets folder.
No, no. What i mean is that the files DO get copied, but they are located INSIDE a jar file, so since they are inside a jar file, you cant access it simply using the path to the file. "Similar" to how yoou cant access a file inside a zip. Because they are inside the jar you need to use the UnityWebRequest class to retrieve the DB,
should be something like this, this is just pseudocode it is not tested
string path = Application.strea$$anonymous$$gAssetsPath;
path += "/" + DATABASE_NA$$anonymous$$E;
using (UnityWebRequest www = UnityWebRequest.Get(path))
{
yield return www.SendWebRequest();
File.WriteAllText(Application.persistentDataPath + "DATABASE_NA$$anonymous$$E ", www.downloadHandler.text);
}
db_connection = new SqliteConnection(Application.persistentDataPath + "DATABASE_NA$$anonymous$$E ");
db_connection.Open();