views:

765

answers:

3
(BOOL) addticket {
    NSString *event=@"max";
    NSString *venue=@"tvm";
    sqlite3 *database;
    databaseName = @"smbhDB.sql";
    sqlite3_stmt *addStatement ;
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
    if(addStatement == nil) {   
        const char *sql ="insert into tickets (venue, event,) Values (?,?)";

        if(sqlite3_prepare_v2(database, sql, -1, &addStatement, NULL) != SQLITE_OK)
            NSAssert1(0, @"444 Error while creating add  statement. '%s'", sqlite3_errmsg(database));
    }

    sqlite3_bind_text(addStatement, 1, [venue UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(addStatement, 2, [event UTF8String], -1, SQLITE_TRANSIENT);

    if(SQLITE_DONE != sqlite3_step(addStatement))
        NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));

    sqlite3_reset(addStatement);
...

This code not working. Please can someone help me to correct it. If anybody can post sample code please upload.

+1  A: 

I'm not 100% sure, because there's no error detail provided, but it looks like addStatement is not being initialized. Does Objective-C initialize all pointers to 0 when they're allocated on the stack? I know that in Linux that would not work.

Perhaps try this: sqlite3_stmt *addStatement = nil;

I have to wonder, why are you even checking addStatement == nil? There's no reason to check it, since you must initialize it in every case.

In short, re-write like so:

(BOOL) addticket {
    NSString *event=@"max";
    NSString *venue=@"tvm";
    sqlite3 *database;
    databaseName = @"smbhDB.sql";
    sqlite3_stmt *addStatement ;
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    const char *sql ="insert into tickets (venue, event,) Values (?,?)";

    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

 /* this is no longer conditional code */
    if(sqlite3_prepare_v2(database, sql, -1, &addStatement, NULL) != SQLITE_OK)
        NSAssert1(0, @"444 Error while creating add  statement. '%s'", sqlite3_errmsg(database));

    sqlite3_bind_text(addStatement, 1, [venue UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(addStatement, 2, [event UTF8String], -1, SQLITE_TRANSIENT);

    if(SQLITE_DONE != sqlite3_step(addStatement))
        NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));

    sqlite3_reset(addStatement);
...
Chris R
+3  A: 

Dublicate? http://stackoverflow.com/questions/862378/sqllite3-prepare-insert-query-in-iphone-programming/862832#862832

YOU MUST OPEN DATABASE before execute statement

oxigen
+1  A: 

Hi Raju,

Consider FMDB or the Omni core data like library. All the details are hidden behind a nice easy abstract interface.

http://gusmueller.com/blog/archives/2008/03/fmdb_for_iphone.html

or

http://www.omnigroup.com/developer (OmniDataObjects)

Good luck.

Bill Dudney