1

I have 2 tables on same database.

For first table data is inserted successfully using below code:

       NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);
       NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"test.sqlite"];
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {

(sqlite3_exec(database, "BEGIN TRANSACTION", 0, 0, 0))==SQLITE_OK

   if ( sqlite3_exec(database, [insertStatement UTF8String], NULL, NULL, &error) == SQLITE_OK)
        {

            NSLog(@"------- inserted here ----");
        }
        else
        {
            NSLog(@"Error: %s", error);

     if(sqlite3_exec(database, "COMMIT", 0, 0, 0) ==SQLITE_OK){
           NSLog(@"-------data sucessfully inserted ");
      }
      else {
         NSLog(@"-------data insertion failed");
          sqlite3_exec(database, "ROLLBACK", 0, 0, 0);
      }

       (sqlite3_exec(database, "END TRANSACTION", 0, 0, 0));
        sqlite3_close(database);


}
else {
   NSLog(@"-------sqlite open error ");
}

The data is inserted successfully as I log.

But when I use the same code for inserting into second table, no data is inserted I get log of

NSLog(@"-------data insertion failed")

I am also opening and closing database at each operation and releasing the handle.

I think sqlite is not allowing me to write on database but don't understand why? Since I have closed database with earlier operation.

Does anyone encountered same problem with no data insertion for 2nd table on same database file.

Addt note: there are no syntax errors and if I don't insert on 1st table then insertion is done on 2nd table which is weird.

1
  • check your query ? whether it is proper or not. Commented Aug 17, 2012 at 12:42

1 Answer 1

3

Try with this code,

-(NSString*) GetDatabasePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
        NSUserDomainMask, YES) ;
    NSString *documentsDirectory = [paths objectAtIndex:0] ;
    return [documentsDirectory stringByAppendingPathComponent:@"test.sqlite"] ;
}

-(void)InsertPurchase 
{
        sqlite3_stmt *statement=nil;
        NSString *path = [self GetDatabasePath];
        NSString *query;

        if(sqlite3_open([path UTF8String],&db) == SQLITE_OK)
        {
            query = [NSString stringWithFormat: @"Your insert query"];

            if(sqlite3_prepare_v2(db, [query UTF8String], -1, &statement, NULL)
                        == SQLITE_OK)
            {
                sqlite3_step(statement);
            }
            sqlite3_finalize(statement);
        }
        sqlite3_close(db);
}
Sign up to request clarification or add additional context in comments.

3 Comments

i am using the same just additional transactions for faster execution, but my q is why my second table is not getting inserted for the same db? Even after closing end ending the transactions after first insertion? Moreover if i override first then second table works well, is it something to do with write issues within same db but in diff. table
i happen to replace my ( sqlite3_exec(database, [insertStatement UTF8String], NULL, NULL, &error) == SQLITE_OK) with your prepare statment and it works. Thankx Krunal
accepting ans means click on the check mark which is on left side of my answer, when you click it will turn green. see here for e.g.:meta.stackexchange.com/questions/5234/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.