Is it possible to insert multiple rows at a time in an SQLite database?

后端 未结 24 2883
猫巷女王i
猫巷女王i 2020-11-21 06:12

In MySQL you can insert multiple rows like this:

INSERT INTO \'tablename\' (\'column1\', \'column2\') VALUES
    (\'data1\', \'data2\'),
    (\'data1\', \'da         


        
相关标签:
24条回答
  • 2020-11-21 06:38

    you can use InsertHelper, it is easy and fast

    documentation: http://developer.android.com/reference/android/database/DatabaseUtils.InsertHelper.html

    tutorial: http://www.outofwhatbox.com/blog/2010/12/android-using-databaseutils-inserthelper-for-faster-insertions-into-sqlite-database/

    Edit: InsertHelper is deprecated as of API Level 17

    0 讨论(0)
  • 2020-11-21 06:39

    Alex is correct: the "select ... union" statement will lose the ordering which is very important for some users. Even when you insert in a specific order, sqlite changes things so prefer to use transactions if insert ordering is important.

    create table t_example (qid int not null, primary key (qid));
    begin transaction;
    insert into "t_example" (qid) values (8);
    insert into "t_example" (qid) values (4);
    insert into "t_example" (qid) values (9);
    end transaction;    
    
    select rowid,* from t_example;
    1|8
    2|4
    3|9
    
    0 讨论(0)
  • 2020-11-21 06:40

    Yes, as of SQLite 3.7.11 this is supported in SQLite. From the SQLite documentation:

    SQLite INSERT statement syntax

    (when this answer was originally written, this was not supported)

    For compatibility with older versions of SQLite, you can use the trick suggested by andy and fearless_fool using UNION, but for 3.7.11 and later the simpler syntax described in here should be preferred.

    0 讨论(0)
  • 2020-11-21 06:40

    As the other posters have said, SQLite does not support this syntax. I don't know if compound INSERTs are part of the SQL standard, but in my experience they're not implemented in many products.

    As an aside, you should be aware that INSERT performance in SQLite is improved considerably if you wrap multiple INSERTs in an explicit transaction.

    0 讨论(0)
  • 2020-11-21 06:40

    If you are using bash shell you can use this:

    time bash -c $'
    FILE=/dev/shm/test.db
    sqlite3 $FILE "create table if not exists tab(id int);"
    sqlite3 $FILE "insert into tab values (1),(2)"
    for i in 1 2 3 4; do sqlite3 $FILE "INSERT INTO tab (id) select (a.id+b.id+c.id)*abs(random()%1e7) from tab a, tab b, tab c limit 5e5"; done; 
    sqlite3 $FILE "select count(*) from tab;"'
    

    Or if you are in sqlite CLI, then you need to do this:

    create table if not exists tab(id int);"
    insert into tab values (1),(2);
    INSERT INTO tab (id) select (a.id+b.id+c.id)*abs(random()%1e7) from tab a, tab b, tab c limit 5e5;
    INSERT INTO tab (id) select (a.id+b.id+c.id)*abs(random()%1e7) from tab a, tab b, tab c limit 5e5;
    INSERT INTO tab (id) select (a.id+b.id+c.id)*abs(random()%1e7) from tab a, tab b, tab c limit 5e5;
    INSERT INTO tab (id) select (a.id+b.id+c.id)*abs(random()%1e7) from tab a, tab b, tab c limit 5e5;
    select count(*) from tab;
    

    How does it work? It makes use of that if table tab:

    id int
    ------
    1
    2
    

    then select a.id, b.id from tab a, tab b returns

    a.id int | b.id int
    ------------------
        1    | 1
        2    | 1
        1    | 2
        2    | 2
    

    and so on. After first execution we insert 2 rows, then 2^3=8. (three because we have tab a, tab b, tab c)

    After second execution we insert additional (2+8)^3=1000 rows

    Aftern thrid we insert about max(1000^3, 5e5)=500000 rows and so on...

    This is the fastest known for me method of populating SQLite database.

    0 讨论(0)
  • 2020-11-21 06:41

    Yes it is possible, but not with the usual comma-separated insert values.

    Try this...

    insert into myTable (col1,col2) 
         select aValue as col1,anotherValue as col2 
         union select moreValue,evenMoreValue 
         union...
    

    Yes, it's a little ugly but easy enough to automate the generation of the statement from a set of values. Also, it appears you only need to declare the column names in the first select.

    0 讨论(0)
提交回复
热议问题