gpt4 book ai didi

sqlite - 如何使用 SQLite 批量插入?

转载 作者:IT王子 更新时间:2023-10-29 06:23:43 24 4
gpt4 key购买 nike

如何使用 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 错误没有这样的列: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();

最佳答案

我编写了一个类来帮助简化 SQLite 中的批量插入。希望对您有所帮助:

http://procbits.com/2009/09/08/sqlite-bulk-insert/

-日本

关于sqlite - 如何使用 SQLite 批量插入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/928873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com