Reputation: 3253
I'm trying to insert some data in a SQLite 3 database in an iOS app. But i keep getting an error that is useless to me.
This is my code trying to insert:
@try {
NSString *dbPath = [self.GetDocumentDirectory stringByAppendingPathComponent:@"knhb.sqlite"];
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
NSLog(@"Cannot locate database file '%@'.", dbPath);
}
if((sqlite3_open_v2([dbPath UTF8String], &db, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK))
{
NSLog(@"An error has occured.");
}
NSString *sqlString = [NSString stringWithFormat:@"INSERT INTO Profile (name, password) VALUES ('%@','%@')", name, password];
const char *sql = [sqlString UTF8String];
sqlite3_stmt *sqlStatement;
if(sqlite3_prepare_v2(db, sql, 1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(@"%s SQLITE_ERROR '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(db), sqlite3_errcode(db));
}
sqlite3_step(sqlStatement);
sqlite3_finalize(sqlStatement);
int lastInsert = sqlite3_last_insert_rowid(db);
NSLog(@"lastinsertid:%d", lastInsert);
sqlite3_close(db);
}
@catch (NSException *exception) {
NSLog(@"An exception occured: %@", [exception reason]);
}
My database table Profile has three columns idProfile, name and password. But since idProfile is an int and primary key I don't have to include it because it is auto incremented.
The error I keep getting is: SQLITE_ERROR 'near "I": syntax error' (1)
I am writing to the Documents folder.
Any help is greatly appreciated.
Daan
Upvotes: 0
Views: 1706
Reputation: 3253
I'm not quite sure why but i fixed it by changing the following line of code:
if(sqlite3_prepare_v2(db, sql, 1, &sqlStatement, NULL) != SQLITE_OK)
i've turned it into:
if(sqlite3_prepare_v2(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
Not sure what the 1 or -1 means. Maybe someone can elaborate?
Cheers!
Upvotes: 1