12

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

Simon
  • 7,991
  • 21
  • 83
  • 163

2 Answers2

22

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);
matthewrdev
  • 11,930
  • 5
  • 52
  • 64
  • 4
    Thanks, wish I could upvote several times, took hours to find this post! – user230910 Jan 17 '16 at 18:09
  • 1
    I am getting the same error, ... But SQLite.SQLite3 doesnt have "Config" method, why? I am using "sqlite-net-pcl (1.4.118)" and "Xamarin.Forms(3.1.0)" – Gilberto B. Terra Jr. Aug 27 '18 at 14:45
  • I found that whilst debugging sometimes, the Path magically doesn't exist, then I run again and it works. on and off – Pierre Nov 20 '18 at 08:12
  • 5
    Hilariously, I've just re-discovered this via Google and solved my own issue with my own answer without realising initially... Doh! – matthewrdev Sep 26 '19 at 11:07
0

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

SQLiteAsyncConnection database = new SQLiteAsyncConnection(dbPath, SQLiteOpenFlags.Create);
TPG
  • 2,811
  • 1
  • 31
  • 52