在使用SQLite.Net的Xamarin.Forms应用程序中,以下代码有效:
public class MyClass
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[MaxLength(45)]
public string Name { get; set; }
}
但是当我删除AutoIncrement属性时,在第二个database.Insert上我得到一个类型为Constraint的SQLiteException(即使在重置模拟器之后)。
然而,https://www.sqlite.org/autoinc.html处的SQLite文档强调,出于性能原因,应尽可能避免使用AutoIncrement。文档建议使用ROWID可以避免AutoIncrement。有没有办法在SQLite.Net中访问ROWID?
********编辑:******** 例外的文本是:
In Main DatabaseException: In OnStart DatabaseException: In Initialise DatabaseException: In UpsertAsync<T> ---> SQLite.Net.SQLiteException: Constraint
at SQLite.Net.PreparedSqlLiteInsertCommand.ExecuteNonQuery (System.Object[] source) [0x00000] in <filename unknown>:0
at SQLite.Net.SQLiteConnection.Insert (System.Object obj, System.String extra, System.Type objType) [0x00000] in <filename unknown>:0
--- End of stack trace from previous location where exception was thrown ---
我的插入代码如下:
db = DependencyService.Get<ISQLite> ().GetAsyncConnection ();
MyClass mc = new MyClass()
{
Name = "Hello World!"
};
await UpsertAsync<MyClass> ( mc );
...
...
public async Task<int> UpsertAsync<T> (T row)
// Update a row, of if the row does not exist then insert it
// Returns the Id of the row
{
try
{
using (IDisposable releaser = await locker.LockAsync ())
{
Type t = row.GetType ();
int rowId = Convert.ToInt32 ((t.GetRuntimeProperty("Id")).GetValue (row)); // Get row.Id at runtime using PropertyInfo
if (rowId != 0)
{
await db.UpdateAsync (row).ConfigureAwait(continueOnCapturedContext:false);
return rowId;
}
else
{
await db.InsertAsync (row).ConfigureAwait(continueOnCapturedContext:false);
rowId = Convert.ToInt32 ((t.GetRuntimeProperty ("Id")).GetValue (row));
return rowId;
}
}
}
catch (Exception ex)
{
throw new DatabaseException ( "In UpsertAsync<T> ", ex);
}
}
这在没有AutoIncrement时会出现异常,并且可以与AutoIncrement一起正常工作。
注意:似乎索引从AutoIncrement开始为1而没有它时为0。这暴露了我的Upsert代码中的一个错误,它在没有AutoIncrement的情况下测试rowId为0。但是,该错误与此问题无关。