Monotouch SQLite每次都会创建一个新的数据库吗?

时间:2013-02-14 00:04:38

标签: ios sqlite xamarin.ios

我对此有点新鲜。我想使用https://github.com/praeclarum/sqlite-net/blob/master/examples/Stocks/Stocks.cs

来使用SQLite-net dealio

这是我获得此代码的确切示例。我喜欢能够宣传我的模型非常快速和干净的想法。

然而,我得到了这个。我想知道它是否会创建一个新的数据库以及每次运行的所有内容,如果你从appdelegate类调用它,就像它建议的那样。

new _db = new Database();

这就是它在app委托类中使用的内容,我对它有点吓坏了。我需要数据保持不变。 有人可以告诉我这是否会每次都重新创建,如果是这样,如何创建数据库,我的模型类中的表会将它们保存在只生成一次的普通db文件中。

感谢任何帮助!

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

3 个答案:

答案 0 :(得分:3)

CreateTable()方法确实创建了表(如果它尚不存在),但它不会破坏表中已有的任何数据。

答案 1 :(得分:1)

我稍微弄乱了一下。答案是,如果这些表不存在,它将创建这些表。

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

这使您能够使用MVC模型方法创建所有类,并利用https://github.com/praeclarum/sqlite-net库,同时仍然以编程方式构建db而不使用实际的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 ();
        }
    }
}

答案 2 :(得分: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;
            }
相关问题