Batch insert values to a specific column in sqlite3 on iOS -


i have table in database. want add values specific column in table. since there thousands plus rows in table figured have use batch update. code syntax error , message saying there no such row:

nsstring *addproductcolquery = @"alter table sale add column product text"; [self.dbmanager loaddatafromdb:addproductcolquery];  nsstring *batchstart = @"begin immediate transaction"; [self.dbmanager loaddatafromdb:batchstart]; (int = 0; i<self.productinfo.count; i++) {     nsstring *addvalue = [nsstring stringwithformat:@"insert sale (product) values (%@)",[tempproductarray objectatindex:i]];      [self.dbmanager loaddatafromdb:addvalue]; } nsstring *batchcomit = @"commit transaction"; [self.dbmanager loaddatafromdb:batchcomit]; 

update: have managed above code work getting 90% cpu usage on iphone6! saw coming since im using loop dead wrong loop query. there way batch insert values rows in specific column?

are values in tempproductarray text values? if so, have quote them in sql:

nsstring *addvalue = [nsstring stringwithformat:@"insert sale (product) values ('%@')", tempproductarray[i]]; 

having shown quick solution, that's wrong way it. inadvisable use stringwithformat build sql text values. if of strings have apostrophe characters in them, sql fail. also, if of data provided end-user or network, you're exposed sql injection attacks.

the correct solution use sqlite3_prepare_v2 of sql without quotes, ? placeholder:

nsstring *addvalue = @"insert sale (product) values (?)"; 

and then, before calling sqlite3_step, call sqlite3_bind_text bind text value ? placeholder.

if dbmanager doesn't offer capability, you'll have add it. or use library, fmdb, offers capability out of box.


Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -