将数据插入sqlite内存数据库
本文关键字:内存 数据库 sqlite 插入 数据 | 更新日期: 2023-09-27 18:08:26
所以我有一个内存中的数据库,我试图将数据插入到表中,它不工作。我做错了什么吗?
var connection = new SQLiteConnection("DataSource=:memory:;Version=3;New=True;");
connection.Open();
string sql = "CREATE TABLE recently_viewed(movieid INTEGER,picture TEXT)";
SQLiteCommand command = new SQLiteCommand(sql, connection);
command.ExecuteNonQuery();
using ( command = connection.CreateCommand())
{
command.CommandText = "INSERT into recently_viewed(movieid ,picture) values(@movieid,@picture)";
command.Prepare();
command.Parameters.AddWithValue("@movieid", id);
command.Parameters.AddWithValue("@picture", picture);
}
string count_table = " SELECT count(*) FROM recently_viewed";
SQLiteCommand com3 = new SQLiteCommand(count_table, connection);
int ctable = Convert.ToInt32(com3.ExecuteScalar().ToString());
Response.Write(ctable);
connection.Close();
我刚刚从你的代码中注意到这一行:
command.ExecuteNonQuery();
只被调用一次。当你这样做时会发生什么:
var connection = new SQLiteConnection("DataSource=:memory:;Version=3;New=True;");
connection.Open();
string sql = "CREATE TABLE recently_viewed(movieid INTEGER,picture TEXT)";
SQLiteCommand command = new SQLiteCommand(sql, connection);
command.ExecuteNonQuery();
using ( command = connection.CreateCommand())
{
command.CommandText = "INSERT into recently_viewed(movieid ,picture) values(@movieid,@picture)";
command.Prepare();
command.Parameters.AddWithValue("@movieid", id);
command.Parameters.AddWithValue("@picture", picture);
command.ExecuteNonQuery();
}
string count_table = " SELECT count(*) FROM recently_viewed";
SQLiteCommand com3 = new SQLiteCommand(count_table, connection);
com3.ExecuteNonQuery();
int ctable = Convert.ToInt32(com3.ExecuteScalar().ToString());
Response.Write(ctable);
connection.Close();