2012-07-03 86 views
3

我有一個SQLite的問題。當我嘗試插入條目時出現錯誤。我發現錯誤是「SQLITE MISUSE」,錯誤代碼爲21,使用SQLite「SQLITE MISUSE」錯誤

NSLog(@"ERROR: Failed to add food! (code: %d)",sqlite3_step(statement)); 

insertSQL在我的代碼中的字符串是在需要時正確創建的。另外,我看到了使用iFunBox創建的表格。

這裏是我的插入方法:

-(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); 
}} 

可能創建方法將是有益的:

-(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!"); 
    } 
} 

}

回答

0

我發現了一個奇怪的情況,我在聲明中使用的「定義」這個詞會導致錯誤。有趣的是「定義」字未列在SQLite關鍵字列表中(http://www.sqlite.org/lang_keywords.html

當我將「definiton」替換爲「def」問題解決。 (createDatabase方法)

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)"; 
1

你的日誌記錄邏輯執行兩個sqlite_step()操作,這充其量是誤導。

相反,拍攝第一sqlite_step()調用的返回代碼並報告值:

int rc = sqlite3_step(statement); 
if (rc == SQLITE_OK) 
{ 
    NSLog(@"Food added."); 
} else { 
    NSLog(@"ERROR: Failed to add food!: %d", rc); 
} 

你需要這樣的邏輯延伸到你的代碼中的所有sqlite_xxx()電話。

+0

那麼,你的代碼和我的代碼沒有區別。兩者都導致同樣的錯誤 –

+0

@mete發佈完整的重組代碼。 – trojanfoe

+0

我有一個創造者的方法,但它工作正常。我用iFunBox看到創建的數據庫和表格。我的問題中的方法無法正常工作。 –