我想创建一个.NET应用程序,它将信息存储在SQLite文件中。我遇到的问题是执行SQL命令后,只有一条记录可见,就像当前行被覆盖一样。Sqlite插入覆盖记录
创建表:
bool pathExist = Directory.Exists(appdataPath + "OnCallDB.sqlite");
if (!pathExist)
{
SQLiteConnection.CreateFile(appdataPath + "OnCallDB.sqlite");
SQLiteConnection dbNewConnection = new SQLiteConnection("Data Source=" + appdataPath + "OnCallDB.sqlite;Version=3;");
dbNewConnection.Open();
string sqlTableCreation = "CREATE TABLE Calls (Id Integer PRIMARY KEY, name varchar(30), ticket varchar(20), phone varchar(20), day varchar(20), start_time varchar(20), end_time varchar(20), raw_time varchar(20), weighted_time varchar(20), date DATETIME, notes varchar(255))";
SQLiteCommand command = new SQLiteCommand(sqlTableCreation, dbNewConnection);
command.ExecuteNonQuery();
dbNewConnection.Close();
}
Insert语句:
SQLiteCommand insertCommand = new SQLiteCommand("INSERT INTO Calls ([name], [ticket], [phone], [day], [start_time], [end_time], [raw_time], [weighted_time], [date], [notes])"+
"VALUES(@name, @ticket, @phone, @day, @start_time, @end_time, @raw_time, @weighted_time, @date, @notes)",connection);
insertCommand.Parameters.AddWithValue("@name", name);
insertCommand.Parameters.AddWithValue("@ticket", ticket);
insertCommand.Parameters.AddWithValue("@phone", phone);
insertCommand.Parameters.AddWithValue("@day", day);
insertCommand.Parameters.AddWithValue("@start_time", start_time);
insertCommand.Parameters.AddWithValue("@end_time", end_time);
insertCommand.Parameters.AddWithValue("@raw_time", raw_time);
insertCommand.Parameters.AddWithValue("@weighted_time", weighted_time);
insertCommand.Parameters.AddWithValue("@date", date);
insertCommand.Parameters.AddWithValue("@notes", notes);
try
{
connection.Open();
insertCommand.ExecuteNonQuery();
connection.Close();
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
我已经手动运行该命令的SQL文件,并插入一个新行。当我运行上面的SQLite命令时,它将替换该行。据我所知。我查看了文件的创建日期和时间,看起来该文件刚刚被修改并且没有被替换。
我必须承认我没有看到命令本身的问题。我从来没有听说过一个插入行为作为更新...你检查数据库值,看看第二个插入是否有效地取代第一个? – Jauch 2014-12-02 01:05:06
Sqlite似乎能够在插入时替换,例如,如果违反了密钥,但只有在使用on conflict子句时,我认为。试图用同样的钥匙包括第二个重新加入的乐曲时,你是否会遇到异常情况?什么是Id字段?我认为你错过了插入的ID和默认值将违反密钥 – Jauch 2014-12-02 01:14:15
你不是多次运行表创建方法......对吧?我担心的是,根据代码和设计的结构,您有可能覆盖表格。代码中没有指向REPLACE冲突解决算法或ON CONFLICT子句。我甚至不知道会有什么影响......考虑到我只知道DROP TABLE,嗯。 – 2014-12-02 04:56:46