2

I am trying to insert some data sqlite database...I am sure that the insert query is correct as I tested that from terminal and sqlite manager but it is not inserted from ios5...please help!!!

-(BOOL)insertWithRawQuery:(NSString*) query
{
        sqlite3_stmt    *statement;
        BOOL success = NO;
        if (sqlite3_open([databaseFilePath UTF8String], &database) == SQLITE_OK)
        {
            NSLog(@"%@",query);
            const char *insert_stmt = [query UTF8String];

            sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL);
            if (sqlite3_step(statement) == SQLITE_DONE)
            {
                success=YES;
            } else 
            {
                success=NO;
                NSLog(@"%d",sqlite3_step(statement));
            }
            sqlite3_finalize(statement);
       }
         sqlite3_close(database);
        return success;
}

    NSLog(@"%d",sqlite3_step(statement))

printing 21 which is SQLITE_MISUSE..do you have any idea of that?

Jitendra
  • 5,055
  • 2
  • 22
  • 42
Shoeb Amin
  • 599
  • 1
  • 8
  • 18
  • What's the value of `databaseFilePath`? Is it a writable location? – Phillip Mills Jun 29 '12 at 16:47
  • Have you tested that: sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL) is returning SQLITE_OK or not ? If not, please have a look on this. If it is not returning SQLITE_OK then you can print the log in your console as follows: NSLog( @"Error while inserting '%s'", sqlite3_errmsg(database)); It will be more helpful if you print the error logs with sqlite3_errmsg(database) rather than the error code. – Erfan Jun 29 '12 at 16:48
  • databaseFilePath is a writable path... @Erfan I am checking this – Shoeb Amin Jun 29 '12 at 17:00
  • strangle errmsg is printing: Error while inserting 'not an error' – Shoeb Amin Jun 29 '12 at 17:07
  • Really strange. As it is printing Error while inserting and the error is 'not an error'...!!!! Please have a try with my codes below and let me know if it works. :-) – Erfan Jun 29 '12 at 17:13
  • actually I checked that in an if condition and forgot to put "==sqlite_ok"..it is returning ok now..but sqlite3_step(statement) is not returning sqlite_done I have separate functions for select and delete query..that's working fine...I will try your code – Shoeb Amin Jun 29 '12 at 17:19

2 Answers2

7

This is how I've inserted data into sqlite in iOS 5 and it is working fine.

-(void) checkAndCreateDatabase{
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    success = [fileManager fileExistsAtPath:databasePath];
    if(success) return;
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}

-(void) openDatabase
{   
    NSArray *documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDir = [documentsPaths objectAtIndex:0];   
    databasePath = [documentDir stringByAppendingPathComponent:YOUR_DATABASE_NAME];
    [self checkAndCreateDatabase];  
    if(sqlite3_open([databasePath UTF8String],&database) == SQLITE_OK)
        NSLog(@"Database opened.");
}

-(BOOL)insertDataWithUsername:(NSString*)name withPassword:(NSString*)password withAge:(int)age
{
    [self openDatabase];
    NSString *query = nil;
    query = [NSString stringWithFormat:@"INSERT INTO your_table(name, password, age) Values('%@','%@', %d)", name, password, age];

    sqlite3_stmt *compiledStatement;
    if(sqlite3_prepare_v2(database, [query UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK) 
    {   
        if(SQLITE_DONE != sqlite3_step(compiledStatement))
            NSLog( @"Error while inserting data: '%s'", sqlite3_errmsg(database));
        else NSLog(@"New data inserted");

        sqlite3_reset(compiledStatement);       
    }else
    {
        NSLog( @"Error while inserting '%s'", sqlite3_errmsg(database));
    }
    sqlite3_finalize(compiledStatement);
}
Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
Erfan
  • 1,284
  • 1
  • 13
  • 21
  • 1
    now i find out the exact error and resolved thnx... this is the statement was showed my error NSLog( @"Error while inserting '%s'", sqlite3_errmsg(database)); – Karthik Apr 13 '13 at 08:44
0

I think the query that is sent as parameter is the source of problem. The query construction may have used some variables that are auto released or something. Please use XCode->Product Tab-> profile-> select NSZombie-> and run the program to check if there are any objects used to construct the query are auto released or not.