0

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.

6
  • Any reason you're talking directly to sqlite and not bothering with Core Data? Commented Jun 14, 2011 at 19:39
  • 1
    I would highly recommend using Gus Mueller's FMDatabase wrapper classes around SQLite. github.com/ccgus/fmdb Commented Jun 14, 2011 at 19:40
  • I want to be able to download/upload the database to a server, so it's not restrictive. And to be honest, I never really considered it :/ Commented Jun 14, 2011 at 19:41
  • 2
    Only masochists use the SQLite C API directly in Objective-C. Use FMDB (a SQLite wrapper) or CoreData (an object graph manager) instead. Commented Jun 14, 2011 at 19:42
  • @Dave DeLong: real non-masochists use NSCoding, NSKeyedArchiver, NSKeyedUnarchiver, NSArray and NSSet. Commented Jun 14, 2011 at 21:09

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

Yeah it does have a table called data, and okay i'll check for that.
I'm struggling to find the code that calls what the Sqlite3_step function returns... - as i say i'm a complete novice with SQLite :/
I ran this command: NSLog( @"Insert into row id = %d", sqlite3_last_insert_rowid(database)); The answer that it returned was 0.

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.