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

后端 未结 24 2882
猫巷女王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:48

    I am able to make the query dynamic. This is my table:

    CREATE TABLE "tblPlanner" ("probid" text,"userid" TEXT,"selectedtime" DATETIME,"plannerid" TEXT,"isLocal" BOOL,"applicationid" TEXT, "comment" TEXT, "subject" TEXT)

    and I'm getting all data through a JSON, so after getting everything inside an NSArray I followed this:

        NSMutableString *query = [[NSMutableString alloc]init];
        for (int i = 0; i < arr.count; i++)
        {
            NSString *sqlQuery = nil;
            sqlQuery = [NSString stringWithFormat:@" ('%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@'),",
                        [[arr objectAtIndex:i] objectForKey:@"plannerid"],
                        [[arr objectAtIndex:i] objectForKey:@"probid"],
                        [[arr objectAtIndex:i] objectForKey:@"userid"],
                        [[arr objectAtIndex:i] objectForKey:@"selectedtime"],
                        [[arr objectAtIndex:i] objectForKey:@"isLocal"],
                        [[arr objectAtIndex:i] objectForKey:@"subject"],
                        [[arr objectAtIndex:i] objectForKey:@"comment"],
                        [[NSUserDefaults standardUserDefaults] objectForKey:@"applicationid"]
                        ];
            [query appendString:sqlQuery];
        }
        // REMOVING LAST COMMA NOW
        [query deleteCharactersInRange:NSMakeRange([query length]-1, 1)];
    
        query = [NSString stringWithFormat:@"insert into tblPlanner (plannerid, probid, userid, selectedtime, isLocal, applicationid, subject, comment) values%@",query];
    

    And finally the output query is this:

    insert into tblPlanner (plannerid, probid, userid, selectedtime, isLocal, applicationid, subject, comment) values 
    <append 1>
    ('pl1176428260', '', 'US32552', '2013-06-08 12:00:44 +0000', '0', 'subj', 'Hiss', 'ap19788'),
    <append 2>
    ('pl2050411638', '', 'US32552', '2013-05-20 10:45:55 +0000', '0', 'TERI', 'Yahoooooooooo', 'ap19788'), 
    <append 3>
    ('pl1828600651', '', 'US32552', '2013-05-21 11:33:33 +0000', '0', 'test', 'Yest', 'ap19788'),
    <append 4>
    ('pl549085534', '', 'US32552', '2013-05-19 11:45:04 +0000', '0', 'subj', 'Comment', 'ap19788'), 
    <append 5>
    ('pl665538927', '', 'US32552', '2013-05-29 11:45:41 +0000', '0', 'subj', '1234567890', 'ap19788'), 
    <append 6>
    ('pl1969438050', '', 'US32552', '2013-06-01 12:00:18 +0000', '0', 'subj', 'Cmt', 'ap19788'),
    <append 7>
    ('pl672204050', '', 'US55240280', '2013-05-23 12:15:58 +0000', '0', 'aassdd', 'Cmt', 'ap19788'), 
    <append 8>
    ('pl1019026150', '', 'US32552', '2013-06-08 12:15:54 +0000', '0', 'exists', 'Cmt', 'ap19788'), 
    <append 9>
    ('pl790670523', '', 'US55240280', '2013-05-26 12:30:21 +0000', '0', 'qwerty', 'Cmt', 'ap19788')
    

    which is running well through code also and I'm able to save everything in SQLite successfully.

    Before this i made UNION query stuff dynamic but that started giving some syntax error. Anyways, this is running well for me.

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

    According to this page it is not supported:

    • 2007-12-03 : Multi-row INSERT a.k.a. compound INSERT not supported.
      INSERT INTO table (col1, col2) VALUES 
          ('row1col1', 'row1col2'), ('row2col1', 'row2col2'), ...
    

    Actually, according to the SQL92 standard, a VALUES expression should be able to stand on itself. For example, the following should return a one-column table with three rows: VALUES 'john', 'mary', 'paul';

    As of version 3.7.11 SQLite does support multi-row-insert. Richard Hipp comments:

    "The new multi-valued insert is merely syntactic suger (sic) for the compound insert. There is no performance advantage one way or the other."

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

    in mysql lite you cannot insert multiple values, but you can save time by opening connection only one time and then doing all insertions and then closing connection. It saves a lot of time

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

    If you use the Sqlite manager firefox plugin, it supports bulk inserts from INSERT SQL statements.

    Infact it doesn't support this, but Sqlite Browser does (works on Windows, OS X, Linux)

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

    update

    As BrianCampbell points out here, SQLite 3.7.11 and above now supports the simpler syntax of the original post. However, the approach shown is still appropriate if you want maximum compatibility across legacy databases.

    original answer

    If I had privileges, I would bump river's reply: You can insert multiple rows in SQLite, you just need different syntax. To make it perfectly clear, the OPs MySQL example:

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

    This can be recast into SQLite as:

         INSERT INTO 'tablename'
              SELECT 'data1' AS 'column1', 'data2' AS 'column2'
    UNION ALL SELECT 'data1', 'data2'
    UNION ALL SELECT 'data1', 'data2'
    UNION ALL SELECT 'data1', 'data2'
    

    a note on performance

    I originally used this technique to efficiently load large datasets from Ruby on Rails. However, as Jaime Cook points out, it's not clear this is any faster wrapping individual INSERTs within a single transaction:

    BEGIN TRANSACTION;
    INSERT INTO 'tablename' table VALUES ('data1', 'data2');
    INSERT INTO 'tablename' table VALUES ('data3', 'data4');
    ...
    COMMIT;
    

    If efficiency is your goal, you should try this first.

    a note on UNION vs UNION ALL

    As several people commented, if you use UNION ALL (as shown above), all rows will be inserted, so in this case, you'd get four rows of data1, data2. If you omit the ALL, then duplicate rows will be eliminated (and the operation will presumably be a bit slower). We're using UNION ALL since it more closely matches the semantics of the original post.

    in closing

    P.S.: Please +1 river's reply, as it presented the solution first.

    0 讨论(0)
  • 2020-11-21 06:59
    INSERT INTO tabela(coluna1,coluna2) 
    SELECT 'texto','outro'
    UNION ALL 
    SELECT 'mais texto','novo texto';
    
    0 讨论(0)
提交回复
热议问题