如何使用 SQLite 批量插入?
我查了一下,好像我用 select 语句做了一个插入。我搜索了一下,查看了示例,它们看起来都像是在将数据从一个表复制到另一个表,或者与 SQLite 不兼容。我想做类似的事情
"INSERT INTO user_msg_media (recipientId, mediaId, catagory, current_media_date) " +
"VALUES(@mediaId, @catagory, @current_media_date)";
where the value of recipientId is the watcher from each of
"SELECT watcher FROM userwatch WHERE watched=@watched";
我尝试了下面的代码,但收到错误“SQLite error no such column: watcher”
command.CommandText =
"CREATE TABLE if not exists user_msg_media( " +
"msgId INTEGER PRIMARY KEY, " +
"recipientId INTEGER, " +
"mediaId INTEGER, " +
"catagory INTEGER, " +
"current_date DATE);";
command.ExecuteNonQuery();
//user media
command.CommandText =
"CREATE TABLE if not exists user_watch( " +
"indx INTEGER PRIMARY KEY, " +
"watcher INTEGER, " +
"watched INTEGER);";
command.ExecuteNonQuery();
//...
command.CommandText = "SELECT watcher FROM user_watch WHERE watched=:watched;";
command.Parameters.Add(":watched", DbType.Int64).Value = 1;
command.ExecuteNonQuery(); //is ok
command.CommandText =
"INSERT INTO user_msg_media (recipientId, mediaId, catagory, current_media_date) " +
"SELECT watcher, :mediaId, :category, :current_media_date" +
"FROM user_watch WHERE watched=:watched;";
command.Parameters.Add(":mediaId", DbType.Int64).Value = 0;
command.Parameters.Add(":category", DbType.Int64).Value = 0;
command.Parameters.Add(":current_media_date", DbType.Int64).Value = 0;
command.Parameters.Add(":watched", DbType.Int64).Value = 1;
command.ExecuteNonQuery();