I have a problem with SQLite. When I try to insert entries I get an error. I found that error is "SQLITE MISUSE" with error code 21 by using
NSLog(@"ERROR: Failed to add food! (code: %d)",sqlite3_step(statement));
insertSQL string in my code is created properly when needed. Also, I see the created table with iFunBox.
Here is my insert method:
-(void)saveDataWithCategoryNumber:(int)categoryNumber foodNumber:(int)foodNumber foodName:(NSString *)foodName definiton:(NSString *)definiton ingredients:(NSString *)ingredients calorie:(int)calorie price:(int)price image1:(NSString *)image1 image2:(NSString *)image2 image3:(NSString *)image3 image4:(NSString *)image4 {
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &foodDB) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO foodDB (categoryNumber, foodNumber, foodName, definiton, ingredients, calorie, price, image1, image2, image3, image4) VALUES (%i, %i, \"%@\", \"%@\", \"%@\", %i, %i, \"%@\", \"%@\", \"%@\", \"%@\")", categoryNumber, foodNumber, foodName, definiton, ingredients, calorie, price, image1, image2, image3, image4];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(foodDB, insert_stmt, -1, &statement, NULL);
//char *error;
//sqlite3_exec(foodDB, insert_stmt, NULL, NULL, &error);
NSLog(@"insertSQL: %@",insertSQL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(@"Food added.");
} else {
NSLog(@"ERROR: Failed to add food! (code: %d)",sqlite3_step(statement));
}
sqlite3_finalize(statement);
sqlite3_close(foodDB);
}}
May be creation method will be useful:
-(void)createDatabase{
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"foodDB.db"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &foodDB) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS foodDB (ID INTEGER PRIMARY KEY AUTOINCREMENT, categoryNumber INT, foodNumber INT, foodName TEXT, definition TEXT, ingredients TEXT, calorie INT, price INT, image1 TEXT, image2 TEXT, image3 TEXT, image4 TEXT)";
if (sqlite3_exec(foodDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
NSLog(@"ERROR: Failed to create database!");
}
sqlite3_close(foodDB);
} else {
NSLog(@"ERROR: Failed to open/create database!");
}
}
}
I found a weird situation, "definition" word that I used in my statement causes the error. The funny thing is "definition" word is not listed in SQLite keywords list (http://www.sqlite.org/lang_keywords.html)
When I replaced "definiton" with "def" problem solved. (createDatabase method)
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS foodDB (ID INTEGER PRIMARY KEY AUTOINCREMENT, categoryNumber INT, foodNumber INT, foodName TEXT, definition TEXT, ingredients TEXT, calorie INT, price INT, image1 TEXT, image2 TEXT, image3 TEXT, image4 TEXT)";
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS foodDB (ID INTEGER PRIMARY KEY AUTOINCREMENT, categoryNumber INT, foodNumber INT, foodName TEXT, def TEXT, ingredients TEXT, calorie INT, price INT, image1 TEXT, image2 TEXT, image3 TEXT, image4 TEXT)";