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

后端 未结 24 2924
猫巷女王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 
    
    ('pl1176428260', '', 'US32552', '2013-06-08 12:00:44 +0000', '0', 'subj', 'Hiss', 'ap19788'),
    
    ('pl2050411638', '', 'US32552', '2013-05-20 10:45:55 +0000', '0', 'TERI', 'Yahoooooooooo', 'ap19788'), 
    
    ('pl1828600651', '', 'US32552', '2013-05-21 11:33:33 +0000', '0', 'test', 'Yest', 'ap19788'),
    
    ('pl549085534', '', 'US32552', '2013-05-19 11:45:04 +0000', '0', 'subj', 'Comment', 'ap19788'), 
    
    ('pl665538927', '', 'US32552', '2013-05-29 11:45:41 +0000', '0', 'subj', '1234567890', 'ap19788'), 
    
    ('pl1969438050', '', 'US32552', '2013-06-01 12:00:18 +0000', '0', 'subj', 'Cmt', 'ap19788'),
    
    ('pl672204050', '', 'US55240280', '2013-05-23 12:15:58 +0000', '0', 'aassdd', 'Cmt', 'ap19788'), 
    
    ('pl1019026150', '', 'US32552', '2013-06-08 12:15:54 +0000', '0', 'exists', 'Cmt', 'ap19788'), 
    
    ('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.

提交回复
热议问题