SQLite - Could not open database file

谁说我不能喝 提交于 2020-01-22 17:57:05

问题


I am coding a Xamarin Android application, and am getting an error when trying to create a SQLite database.

Here is my code:

string applicationFolderPath = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "CanFindLocation");
string databaseFileName = System.IO.Path.Combine(applicationFolderPath, "CanFindLocation.db");
SQLite.SQLite3.Config(SQLite.SQLite3.ConfigOption.Serialized);
var db = new SQLiteConnection (databaseFileName);

Here is the error that I am getting:

SQLite.SQLiteException: Could not open database file: /data/data/com.xamarin.docs.android.mapsandlocationdemo2/files/CanFindLocation/CanFindLocation.db (CannotOpen)

I have the same code working in another Xamarin application and would like to know if the exception has something to do with the name of the package?

Thanks in advance


回答1:


Does the path folder path that you are providing to SQLite exist? If you haven't created the CanFindLocation folder then opening a connection to that path will fail.

Try:

string applicationFolderPath = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "CanFindLocation");

// Create the folder path.
System.IO.Directory.CreateDirectory(applicationFolderPath);

string databaseFileName = System.IO.Path.Combine(applicationFolderPath, "CanFindLocation.db");
SQLite.SQLite3.Config(SQLite.SQLite3.ConfigOption.Serialized);
var db = new SQLiteConnection (databaseFileName);



回答2:


My problem is related to added the option of "SQLiteOpenFlags.Create" during the initialization.

SQLiteAsyncConnection database = new SQLiteAsyncConnection(dbPath, SQLiteOpenFlags.Create);


来源:https://stackoverflow.com/questions/25882837/sqlite-could-not-open-database-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!