I can not insert a new row to the database. What am I doing wrong?
-(void)InsertRecords{
fileMgr = [NSFileManager defaultManager];
sqlite3_stmt *stmt;
sqlite3 *cruddb;
const char *sql = "Insert into AllDictionary(id, DeleteFlag, TitleBD, Description, PathToDB, ImageURL) VALUES('565','asasf2','asfas2ff','fggaga2hah','b2bb','aa2aa')";
NSString *cruddatabase = [self.GetDocumentDirectory stringByAppendingPathComponent:@"MainDb.sqlite"];
if(!(sqlite3_open([cruddatabase UTF8String], &cruddb)==SQLITE_OK))
{
NSLog(@"An error has occured: %s",sqlite3_errmsg(cruddb));
}
if (sqlite3_prepare(cruddb, sql, -1, &stmt, NULL)!=SQLITE_OK) {
NSLog(@"Problem with prepare statement: %s", sqlite3_errmsg(cruddb));
} else {
sqlite3_step(stmt);
}
sqlite3_finalize(stmt);
sqlite3_close(cruddb);
}
Your Id type is int.
So the problem is with the query,
Don’t use quotes for integer values it’s for string values.
change the query to:
And there are alot of mistakes in your method, like the prepare statement will always executed if the db is not opened. Finalize statement will work always with or without executing the prepare statement.
so re-write it like: