Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite - Could not open database file

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

like image 450
user3736648 Avatar asked Sep 17 '14 05:09

user3736648


People also ask

Why SQLite Cannot open database file?

If SQLite is unable to open the database file, this means that the SQLite database you are trying to open is corrupted. There are various causes of corruption, such as file overwrite issues, file locking issues, database synchronization failures, storage media failures, and many more.

Could not open database file reason database is locked?

Reasons Responsible for Error Database is Locked. This error code occurs when the user tries to perform two inappropriate operations on a database at the same detail and on the same database connection.

What opens an SQLite file?

You can open a SQLITE file using SQLite (cross-platform) or SQLite Database Browser (cross-platform).


1 Answers

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);
like image 97
matthewrdev Avatar answered Sep 30 '22 13:09

matthewrdev