예제 #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
 public void Commit(System.Data.SQLite.SQLiteDatabase db = null)
 {
     if (db == null)
     {
         db = this.db;
     }
     if (db == null)
     {
         return;
     }
     db.Commit();
 }