iPhone - Connection String and DB File iPhone - Connection String and DB File sqlite sqlite

iPhone - Connection String and DB File


Here is what we do:

We ship a copy of the DB in the application. It is included as Content, Always Copy in the project.

On the user's machine, it is stored in the special directory Environment.SpecialFolder.Personal.

When the app is started, we check to see if the database exists on the user's system and, if not, copy it there.

The connection string is just "Data Source=" + sDatabasePath.

Here is a sample of the code that we use for this (I hacked in the connection stuff since we use a homebuilt class for managing the DB, but you should get the idea):

const string DATABASE_FILE_NAME = "MyDB.db3";bool fSuccess = false;DbConnection conn = new DbConnection ();string sApplicationDir = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.Personal), "MyApplicationSubDir");if (!Directory.Exists (sApplicationDir)) {    Directory.CreateDirectory (sApplicationDir);}// Generate the directory to the database filestring sDatabaseDir = Path.Combine (sApplicationDir, "Database");m_sDatabaseDir = sDatabaseDir;if (!Directory.Exists (sDatabaseDir)) {    Directory.CreateDirectory (sDatabaseDir);}// Generate the path to the database filestring sDatabasePath = Path.Combine (sDatabaseDir, DATABASE_FILE_NAME);m_sDatabaseFile = sDatabasePath;// If the file does not not existif (!File.Exists (sDatabasePath)) {    // Copy the base implementation    File.Copy (Path.Combine (Path.Combine (Environment.CurrentDirectory, "Database"), DATABASE_FILE_NAME), sDatabasePath);}// Initialize the DBconn.ConnectionString = "Data Source=" + sDatabasePath;


out of interest, have you looked at sqlite-net ? http://code.google.com/p/sqlite-net/

Makes your DB handling a lot easier.