gunmania0
gunmania0

Reputation: 123

Writing to SQLite Database objective C Iphone SDK

I need help writing new entries into an SQLite database using Objective C and the iPhone SDK.

The open/close/read database operations go okay, but I can't work out why this bit of code is not working.

-(void) testWriteToDatabase{

NSLog(@"instructions sent 3");
    sqlite3 *database;
    sqlite3_stmt *compiledStatement;
const char *insertSql="INSERT INTO data (name,pass,email) VALUES(?,?,?)";

NSString *userRegistered=userRegister.text;
NSLog(@"user %@",userRegistered);
NSString *passRegistered=passRegister.text;
NSLog(@"pass %@",passRegistered);
NSString *emailRegistered=emailRegister.text;
NSLog(@"email %@",emailRegistered);
sqlite3_stmt *insertStmt = nil;



if(sqlite3_open([databasePath UTF8String],&database) == SQLITE_OK) {

    if(sqlite3_prepare_v2(database, insertSql, -1, &insertStmt, NULL) == SQLITE_OK){

NSLog(@"instructions sent 4");
sqlite3_bind_text(insertStmt, 1, [userRegistered UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insertStmt, 2, [passRegistered UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insertStmt, 3, [emailRegistered UTF8String], -1, SQLITE_TRANSIENT);
            NSLog(@"instructions sent 5");
        while (sqlite3_step(compiledStatement) == SQLITE_ROW) {


        }

     }
sqlite3_reset(insertStmt);    
    }
sqlite3_close(database);    
}

To honest, I've never really used SQLite before, but the main problem is it's not adding entries to the database 'data' like i want it to. Help would be greatly appreciated, cheers.

Upvotes: 0

Views: 1531

Answers (2)

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

Shouldn't

while (sqlite3_step(compiledStatement) == SQLITE_ROW) {
}

be

while (sqlite3_step(insertStmt) == SQLITE_ROW) {
}

as you've used insertStmt everywhere else. compiledStatement is undefined as I understand your code.

Upvotes: 3

Seva Alekseyev
Seva Alekseyev

Reputation: 61361

Does the database have a table called "data"? Trivial thing, but still. Also, do check what does the sqlite3_step return on every step.

Upvotes: 0

Related Questions