2013-02-14 66 views
1

我对此有点新。我想使用SQLite-net dealio使用https://github.com/praeclarum/sqlite-net/blob/master/examples/Stocks/Stocks.csMonotouch SQLite会每次创建一个新的数据库吗?

这就是我得到此代码的确切的示例。我喜欢能够宣布我的模型超级快速和干净的想法。

但是,我得到了这个运行。我想知道是否它会创建一个新的数据库,并且每次运行时都会创建一个新的数据库,如果你想从appdelegate类调用它,就像它建议的那样。

new _db = new Database();

这就是它使用在应用程序委托类我就是一个由它有点吓坏了。我需要数据来坚持。 有人可以告诉我,这是否会每次都重新创建,如果是这样,如何创建数据库,我的模型类中的表格将它们保存在只有一次创建的正常数据库文件中。

任何帮助表示赞赏!

public class Database : SQLiteConnection 
{ 
    public Database (string path) : base(path) 
    { 
     CreateTable<Stock>(); 
     CreateTable<Valuation>(); 
    } 
    // more code here 
} 

回答

1

我搞砸了这一点。答案是,如果它们不存在,它将创建这些表。

而不是使用SQLiteConnection以这种方式创建数据库,它更容易使用sqliteconnection并检查数据库是否存在。然后,如果db文件不存在,则有一个方法可以建立你的表。下面有一个代码示例。

这使您可以创建所有类与MVC模式的做法,并采取https://github.com/praeclarum/sqlite-net库的优势,同时仍编程的方式构建的数据库,而无需使用实际的SQL语句

public class Database 
{ 
    static string documents = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments); 
    // I do this so I can access the path without having to create an instance of the datbase class 
    public static string db = Path.Combine (documents, "dbAwesome.db"); 
    static bool exists = File.Exists(db); 

    public void GetConnection() 
    { 
     if (!exists) { 
      SqliteConnection.CreateFile (db); 
      // Custom methods to create initial tables and values for tables 
      CreateMyTables(); 
      InsertDefaultValues(); 
     } 
    } 
} 
3

的CREATETABLE()方法创建该表(如果它不存在),但它不会破坏表中已有的任何数据。

+0

非常清晰和简单的解释。谢谢 – BRogers 2013-02-14 17:44:41

0

SQLite.Net是一个ORM。如果使用它(Sqlite.cs),它不会重新创建表。

“在数据库中执行”创建表(如果不存在)“表示方法摘要。当你看到代码时,如果执行失败,则count将为0,并且代码从现有的ORM映射传递索引创建。

它只是看起来ORM映射,如果它存在的行为不同,如下所示。

它也为ORM创建索引,因为您知道我们应该使用[PrimaryKey]的。通过这种方式调用CRUD操作。

 /// <summary> 
      /// Executes a "create table if not exists" on the database. It also 
      /// creates any specified indexes on the columns of the table. It uses 
      /// a schema automatically generated from the specified type. You can 
      /// later access this schema by calling GetMapping. 
      /// </summary> 
      /// <returns> 
      /// The number of entries added to the database schema. 
      /// </returns> 
      public int CreateTable<T>() 
      { 
        var ty = typeof(T); 

        if (_tables == null) { 
          _tables = new Dictionary<string, TableMapping>(); 
        } 
        TableMapping map; 
        if (!_tables.TryGetValue (ty.FullName, out map)) { 
          map = GetMapping (ty); 
          _tables.Add (ty.FullName, map); 
        } 
        var query = "create table \"" + map.TableName + "\"(\n"; 

        var decls = map.Columns.Select (p => Orm.SqlDecl (p)); 
        var decl = string.Join (",\n", decls.ToArray()); 
        query += decl; 
        query += ")"; 

        var count = 0; 

        try { 
          Execute (query); 
          count = 1; 
        } 
        catch (SQLiteException) { 
        } 

        if (count == 0) { 
          // Table already exists, migrate it 
          MigrateTable (map); 
        } 

        foreach (var p in map.Columns.Where (x => x.IsIndexed)) { 
          var indexName = map.TableName + "_" + p.Name; 
          var q = string.Format ("create index if not exists \"{0}\" on \"{1}\"(\"{2}\")", indexName, map.TableName, p.Name); 
          count += Execute (q); 
        } 

        return count; 
      } 
相关问题