0

I start Xamarin Studio 4.0.3 and create a new solution for MonoDroid application. I am targetting version 2.3 or higher. I include Sqlite.cs from the sqlite-net (latest from site) project. I make sure I have a reference to Mono.Data.SQLite in the project. Then in the OnCreate of the default MainActivity I try to use the code shown in the readme to make a database connection:

string path = Android.OS.Environment.DataDirectory.ToString();

string dbname = path + "/" + "test.db3";
var db = new SQLiteConnection(dbname, SQLiteOpenFlags.Create|SQLiteOpenFlags.ReadWrite ,true);

However it always fails with an exception saying it can't open the database. If I follow Greg Shackles example and use SqliteDbHelper method it works, but I would like to understand what I am doing wrong that sqlite-net connection method is not working. I have a feeling I am missing something simple. I did also try just passing in a filename to SQLiteConnection() as well but when it failed I added the OpenFlags to see if that was the issue.

1 Answer 1

1

I just found out that apparently you're supposed to use the personal folder, instead of the databases folder:

string folder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var db = new SQLiteConnection(Path.Combine(folder, "mydb.db"));
Sign up to request clarification or add additional context in comments.

2 Comments

Ugh I figured it was something simple and sure enough that was it. Yet this is the first answer on the site that states that. I even found this question asked on a few mailing lists. Thanks for answering it for certain.
The problem I have with using the Personal folder, is that every time your update your application, the previous db is deleted and therefore all data lost. Storing it on the SD card, seems to be a more realistic solution, as I don't think users would like to have to re-enter their data ever time they upgrade. Unless there is a way to stop the db being deleted from Personal, if so please let me know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.