Sqlite数据库无法删除对象,失败消息为“0”;无法存储类型:<;类型>&引用;
我正在尝试删除存储在Sqlite数据库中的类型的记录。它失败,并显示以下消息:Sqlite数据库无法删除对象,失败消息为“0”;无法存储类型:<;类型>&引用;,sqlite,xamarin,android-sqlite,system.data.sqlite,Sqlite,Xamarin,Android Sqlite,System.data.sqlite,我正在尝试删除存储在Sqlite数据库中的类型的记录。它失败,并显示以下消息: "Cannot store type: MyNamespace.PanelLog" 我有以下删除记录的方法: public static int DeletePanelLog(int id) { return me.db.DeleteItem<PanelLog>(id); } 以下是来自SQLite的调用堆栈: public int Delete<
"Cannot store type: MyNamespace.PanelLog"
我有以下删除记录的方法:
public static int DeletePanelLog(int id)
{
return me.db.DeleteItem<PanelLog>(id);
}
以下是来自SQLite的调用堆栈:
public int Delete<T>(object primaryKey)
{
var map = GetMapping(typeof(T));
var pk = map.PK;
if (pk == null)
{
throw new NotSupportedException("Cannot delete " + map.TableName + ": it has no PK");
}
var q = string.Format("delete from \"{0}\" where \"{1}\" = ?", map.TableName, pk.Name);
return Execute(q, primaryKey); // in this case, primaryKey is a PanelLog object
}
public int Execute(string query, params object[] args)
{
var cmd = CreateCommand(query, args);
if (TimeExecution)
{
if (_sw == null)
{
_sw = new Stopwatch();
}
_sw.Reset();
_sw.Start();
}
var r = cmd.ExecuteNonQuery();
if (TimeExecution)
{
_sw.Stop();
_elapsedMilliseconds += _sw.ElapsedMilliseconds;
Debug.WriteLine(string.Format("Finished in {0} ms ({1:0.0} s total)", _sw.ElapsedMilliseconds, _elapsedMilliseconds / 1000.0));
}
return r;
}
public int ExecuteNonQuery()
{
if (_conn.Trace)
{
Debug.WriteLine("Executing: " + this);
}
var r = SQLite3.Result.OK;
var stmt = Prepare();
r = SQLite3.Step(stmt);
Finalize(stmt);
if (r == SQLite3.Result.Done)
{
int rowsAffected = SQLite3.Changes(_conn.Handle);
return rowsAffected;
}
else if (r == SQLite3.Result.Error)
{
string msg = SQLite3.GetErrmsg(_conn.Handle);
throw SQLiteException.New(r, msg);
}
else
{
throw SQLiteException.New(r, r.ToString());
}
}
Sqlite3Statement Prepare()
{
var stmt = SQLite3.Prepare2(_conn.Handle, CommandText);
BindAll(stmt);
return stmt;
}
void BindAll(Sqlite3Statement stmt)
{
int nextIdx = 1;
foreach (var b in _bindings)
{
if (b.Name != null)
{
b.Index = SQLite3.BindParameterIndex(stmt, b.Name);
}
else
{
b.Index = nextIdx++;
}
BindParameter(stmt, b.Index, b.Value, _conn.StoreDateTimeAsTicks);
}
}
它似乎试图绑定对象类型,而不是对象的值。这是正确的吗?我能做些什么来解决这个问题吗
编辑:
经过一些进一步的测试,似乎以下工作:
public int Delete(object objectToDelete)
{
var map = GetMapping(objectToDelete.GetType());
var pk = map.PK;
if (pk == null)
{
throw new NotSupportedException("Cannot delete " + map.TableName + ": it has no PK");
}
var q = string.Format("delete from \"{0}\" where \"{1}\" = ?", map.TableName, pk.Name);
return Execute(q, pk.GetValue(objectToDelete));
}
而不是使用:
public int DeleteItem<T>(int id) where T : IBusinessEntity, new()
{
lock (locker) {
#if NETFX_CORE
return Delete(new T() { ID = id });
#else
return Delete<T> (new T () { ID = id });
#endif
}
}
我也遇到了这个问题。实际上,它来自DeleteItem定义中的一行:
return Delete<T> (new T () { ID = id });
返回Delete(newt(){ID=ID});
应该是:
return Delete<T> (id);
返回删除(id);
便捷的解决方案!谢谢。我从中得到了这个ORM-也许你可以在你的问题中添加一两个标签来帮助人们发现它更容易<代码>Xamarin也许?它不是来自SQLite代码。它来自一些(沙马林?)样品。就像这个:这应该是正确的答案,谢谢。这是我在Tasky Pro示例中看到的第二个问题,我将不得不对前面提供的示例稍加说明
public int DeleteItem<T>(int id) where T : IBusinessEntity, new()
{
lock (locker) {
#if NETFX_CORE
return Delete(new T() { ID = id });
#else
return Delete<T> (new T () { ID = id });
#endif
}
}
public int Delete(object objectToDelete)
{
var map = GetMapping(objectToDelete.GetType());
var pk = map.PK;
if (pk == null)
{
throw new NotSupportedException("Cannot delete " + map.TableName + ": it has no PK");
}
var q = string.Format("delete from \"{0}\" where \"{1}\" = ?", map.TableName, pk.Name);
return Execute(q, pk.GetValue(objectToDelete));
}
return Delete<T> (new T () { ID = id });
return Delete<T> (id);