繁体   English   中英

将数据插入sqlite内存数据库

[英]Insert data into sqlite in-memory database

因此,我有一个内存数据库,并且正在尝试将数据插入表中,并且该表无法正常工作。 难道我做错了什么?

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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM