SQLite.Net 是一个 ORM。如果您使用它(Sqlite.cs),它不会重新创建表。
“在数据库上执行“如果不存在则创建表””方法摘要说。正如您在代码中看到的那样;如果执行失败,计数将为 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;
}