C# SQLite上次插入Rowid无效
我找到了几个示例,说明如何从对我的SQLite数据库的sql insert调用中获取最后插入的行id,但我的脚本引发了以下错误:C# SQLite上次插入Rowid无效,c#,sqlite,C#,Sqlite,我找到了几个示例,说明如何从对我的SQLite数据库的sql insert调用中获取最后插入的行id,但我的脚本引发了以下错误: SQLiteException Message = "SQLite error\r\nnear \")\": syntax error" InnerException NULL 下面是我发送的SQL文本以及我如何使用它。显然,我误解了什么。有人能帮我吗 我正在尝试返回刚刚插入的ID号 private static int Save(Dates date, SQLit
SQLiteException
Message = "SQLite error\r\nnear \")\": syntax error"
InnerException
NULL
下面是我发送的SQL文本以及我如何使用它。显然,我误解了什么。有人能帮我吗
我正在尝试返回刚刚插入的ID号
private static int Save(Dates date, SQLiteConnection con) {
// REF: http://www.sqlite.org/c3ref/last_insert_rowid.html
int result = 0;
string sql = "INSERT INTO Dates1 " +
"(ID, TaskID, Last1) " +
"VALUES " +
"((SELECT MAX(ID) FROM Dates1)+1, @TaskID, @Last); " +
"SELECT sqlite3_last_insert_rowid(sqlite3*);";
using (SQLiteCommand cmd = new SQLiteCommand(sql, con)) {
cmd.Parameters.AddWithValue(Dates.AT_TASK, date.TaskID);
cmd.Parameters.AddWithValue(Dates.AT_LAST, date.Last.ToShortDateString());
cmd.CommandText = Dates.SQL_INSERT;
try {
result = cmd.ExecuteNonQuery();
} catch (SQLiteException err) {
result = -1;
LogError("Save(Dates, SQLiteConnection)", err);
}
}
return result;
}
仅供参考:我已经设置了该表,以便使用下面的创建SQL
自动生成ID
,但是该表仅存储ID
值的-1
,除非我手动插入它
public const string SQL_CREATE = "CREATE TABLE Dates1 " +
"(ID INTEGER PRIMARY KEY AUTOINCREMENT, TaskID INTEGER, Last1 TEXT);";
引自:
last\u insert\u rowid()last\u insert\u rowid()函数从调用该函数的数据库连接返回最后一行插入的rowid。last_insert_rowid()SQL函数是sqlite3_last_insert_rowid()C/C++接口函数的包装器
因此,您需要将您的声明更改为:
"SELECT last_insert_rowid();"
因为您所做的是尝试调用C API函数。您使用的是
System.Data.SQLite
()还是其他用于.NET的SQLite包装程序?Shazam!就这样。谢谢我在文件里找不到。