簡體   English   中英

SQLite“SQLITE MISUSE”錯誤

[英]SQLite “SQLITE MISUSE” Error

我有SQLite的問題。 當我嘗試插入條目時,我收到錯誤。 我發現錯誤是“SQLITE MISUSE”,錯誤代碼21使用

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

}

您的日志記錄邏輯正在執行兩個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()調用。

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

當我用“def”替換“definiton”問題解決了。 (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)";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM