예제 #1
0
        public static SQLiteDatabase CreateInMemoryDB(string filename)
        {
            // returns a copy of a database in memory
            // create an empty memory db first
            SQLiteDatabase memdb = new SQLiteDatabase(":memory:");

            try
            {
                // open an existing on disk db
                SQLiteDatabase ondiskdb = new SQLiteDatabase(filename);
                ondiskdb.Open();
                // get user  & schema version
                int user_version   = ondiskdb.GetUserVersion();
                int schema_version = ondiskdb.GetSchemaVersion();
                // get text encoding
                string text_encoding = ondiskdb.GetTextEncoding();
                // get sql for creating structures
                string    sql       = "select sql from sqlite_master where name not like 'sqlite_%'";
                DataTable structure = ondiskdb.Select(sql);
                // get user tables
                sql = "select name from sqlite_master where type = 'table' and name not like 'sqlite_%'";
                DataTable usertables = ondiskdb.Select(sql);
                // crate identically structures in memmory db
                memdb.Open();
                foreach (DataRow row in structure.Rows)
                {
                    memdb.DBCommand.CommandText = row[0].ToString();
                    memdb.DBCommand.ExecuteNonQuery();
                }
                // set user & schema version
                memdb.SetUserVerion(user_version);
                memdb.SetSchemaVerion(schema_version);
                // set text encoding
                memdb.SetTextEncoding(text_encoding);
                user_version  = memdb.GetUserVersion();
                text_encoding = memdb.GetTextEncoding();
                // close on disk db
                ondiskdb.Close();
                // attach on disk db to memory db
                memdb.AttachDatabase(filename, "ondisk");
                memdb.BeginTransaction();
                foreach (DataRow row in usertables.Rows)
                {
                    memdb.DBCommand.CommandText = "insert into main." + row[0].ToString() + " select * from ondisk." + row[0].ToString();
                    memdb.DBCommand.ExecuteNonQuery();
                }
                memdb.Commit();
                memdb.DetachDatabase("ondisk");
                memdb.DiskFileName = filename;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return(memdb);
        }
예제 #2
0
 protected System.Data.SQLite.SQLiteDatabase OpenDatabase(string name, string defaultdatabasedirectory, bool inmemory)
 {
     System.Data.SQLite.SQLiteDatabase db = null;
     try
     {
         // check if database path exists --> create if not
         if (!Directory.Exists(defaultdatabasedirectory))
         {
             Directory.CreateDirectory(defaultdatabasedirectory);
         }
         // check if database is already on disk
         DBPath = defaultdatabasedirectory;
         if (!File.Exists(Path.Combine(DBPath, name)))
         {
             // create one on disk
             System.Data.SQLite.SQLiteDatabase dbn = new System.Data.SQLite.SQLiteDatabase(Path.Combine(DBPath, name));
             // open database
             dbn.Open();
             // set user version
             dbn.SetUserVerion(UserVersion);
             // set auto vacuum mode to full
             dbn.SetAutoVacuum(AUTOVACUUMMODE.FULL);
             dbn.Close();
         }
         // check for in-memory database --> open from disk, if not
         if (inmemory)
         {
             db = System.Data.SQLite.SQLiteDatabase.CreateInMemoryDB(Path.Combine(DBPath, name));
         }
         else
         {
             db = new System.Data.SQLite.SQLiteDatabase(Path.Combine(DBPath, name));
             db.Open();
         }
         // get version info
         int v = db.GetUserVersion();
         // do upgrade stuff here
     }
     catch (Exception ex)
     {
         Console.WriteLine("Error initilalizing database: " + ex.Message);
         throw new TypeInitializationException(this.GetType().Name, ex);
     }
     return(db);
 }