SQLite bulk insert on iPhone not working

Posted by App_beginner on Stack Overflow See other posts from Stack Overflow or by App_beginner
Published on 2010-01-29T00:24:46Z Indexed on 2010/05/06 16:38 UTC
Read the original article Hit count: 290

Filed under:
|
|
|

Hi. I have been struggling with this seeminly easy problem for 48 hours, and I am no closer to a solution. So I was hoping that someone might be able to help me.

I am building a app, that use a combination of a local (SQLite) database and an online database (PHP/MYSQL). The app is nearly finished. Checked for leaks and work like a charm. However the very last part is the part I have struggled with.

On launch, I want the app to check for changes to the online databse, and if there is. I want it to download and parse a xml file containing the changes. Everything is working fine this far. But when I try to bulk insert my parsed data to my database, the app crashes, giving a NSInternalInconsistency error. Due to the database returning SQLITE_MISUSE. I have done a lot of googling, but am still unable to solve my problem. So I am putting the code here, hoping that someone can help me fix this.

And I know that I should have used core data for this. But this is the very last part I am struggling with, and I am very reluctant to changing my entire code now. Core data will have to come in the update.

Here is the error I recieve:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error while inserting data. 'library routine called out of sequence''

Here is my code:

-(void)UpdateDatabase:(const char *)_query NewValues:(NSMutableArray *)_odb dbn:(NSString *)_dbn dbp:(NSString *)_dbp  
{  
sqlite3 *database;  
NSMutableArray *NewValues = _odb;  
int i;  
const char *query = _query;  
sqlite3_stmt *addStmt;  
for (i = 1; i < [NewValues count]; i++)  
{  
if(sqlite3_prepare_v2(database, query, -1, &addStmt, NULL) == SQLITE_OK)   
{  
sqlite3_bind_text(addStmt, 1, [[[NewValues objectAtIndex:i] name] UTF8String], -1, SQLITE_TRANSIENT);  
sqlite3_bind_text(addStmt, 2, [[[NewValues objectAtIndex:i] city]UTF8String], -1, SQLITE_TRANSIENT);  
sqlite3_bind_double(addStmt, 3, [[[NewValues objectAtIndex:i] lat] doubleValue]);  
sqlite3_bind_int(addStmt, 4, [[[NewValues objectAtIndex:i] long] doubleValue]);  
sqlite3_bind_int(addStmt, 5, [[[NewValues objectAtIndex:i] code] intValue]);  
}  
if(SQLITE_DONE != sqlite3_step(addStmt)) {  
NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));  
}  
//Reset the add statement.  
sqlite3_reset(addStmt);  
}  
}  

© Stack Overflow or respective owner

Related posts about iphone

Related posts about sqlite