如何使用SQLite批量插入?

时间:2009-05-30 02:53:35

标签: sqlite ado.net bulkinsert

如何批量插入SQLite?

我查了一下,好像我用select语句插入。我用Google搜索,看了一下示例,他们看起来都像是将数据从一个表复制到另一个表,或者与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();

3 个答案:

答案 0 :(得分:8)

我编写了一个类来帮助在SQLite中实现批量插入。希望它有用:

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

-jP

答案 1 :(得分:2)

SQlite不支持@variable表示法,但是(为了清楚起见,使用sqlite的Python绑定支持的命名占位符样式)这应该有效:

INSERT INTO user_msg_media (userId, mediaId, catagory, current_media_date)
SELECT watcher, :mediaId, :category, :current_media_date
FROM userwatch WHERE watched=:watched

编辑:SQLite似乎误解了哪个列名错了。由于列名全部修复,以下Python代码适用于我(不确定您使用的是其他语言,Python对我来说最方便与sqlite交互):

import sqlite3 as sq

con = sq.connect(':memory:')
cur = con.cursor()
cur.execute("CREATE TABLE if not exists user_msg_media( " +
            "msgId        INTEGER PRIMARY KEY, " +
            "recipientId  INTEGER, " +
            "mediaId      INTEGER, " +
            "catagory     INTEGER, " +
            "current_date DATE)")
cur.execute("CREATE TABLE if not exists user_watch( " +
            "indx INTEGER PRIMARY KEY, " +
            "watcher INTEGER, " +
            "watched INTEGER)")

cur.execute("INSERT INTO user_watch VALUES (1, 2, 3)")

cur.execute("SELECT watcher FROM user_watch WHERE watched=:watched",
            dict(watched=3))
print cur.fetchall()

print cur.execute("INSERT INTO user_msg_media (recipientId, mediaId, catagory, current_date) " +
        "SELECT watcher, :mediaId, :category, :current_media_date " +
        "FROM user_watch WHERE watched=:watched;",
        dict(mediaId=0, category=0, current_media_date=0, watched=3)
)

cur.execute("SELECT * FROM user_msg_media")
print cur.fetchall()

但是,如果我在SQL中重现不匹配,例如current_date与current_media_date,我可以错误地诊断错误的列是watcher,即使该列实际上很好。想要尝试将更正后的代码重新用于您喜欢的语言并查看其行为方式吗?

答案 2 :(得分:1)

我建议在StackOverflow上使用朋友的Dapper ORM。如果你看一下性能部分,你就不会比Dapper快得多。

多次执行命令
相同的签名还允许您方便有效地多次执行命令(例如批量加载数据)

使用示例:

connection.Execute(@"insert MyTable(colA, colB) values (@a, @b)",
    new[] { new { a=1, b=1 }, new { a=2, b=2 }, new { a=3, b=3 } }
  ); // 3 rows inserted: "1,1", "2,2" and "3,3"

这适用于为某些T实现IEnumerable的任何参数。