【问题标题】:SQLite "SQLITE MISUSE" ErrorSQLite“SQLITE 误用”错误
【发布时间】:2012-07-03 23:36:26
【问题描述】:

我对 SQLite 有疑问。当我尝试插入条目时出现错误。我发现错误是“SQLITE MISUSE”,使用错误代码 21

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

在我的代码中插入 SQL 字符串在需要时正确创建。另外,我看到了用 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!");
    }
}

}

【问题讨论】:

    标签: iphone xcode sqlite ios5


    【解决方案1】:

    就我而言,我正在使用

    sqlite3_exec

    而不是

    sqlite3_prepare_v2

    这是导致错误的原因。更改 SQL 语句解决了这个问题。

    【讨论】:

      【解决方案2】:

      我发现了一个奇怪的情况,我在声明中使用的“定义”词导致了错误。有趣的是 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)";
      

      【讨论】:

        【解决方案3】:

        您的日志记录逻辑正在执行两个 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() 调用。

        【讨论】:

        • 好吧,你的代码和我的没有区别。两个结果相同的错误。
        • 我有一个创建者方法,但它工作正常。我用 iFunBox 看到了创建的数据库和表。我的问题中的方法无法正常工作。
        • @mete 代码已损坏,如我的回答中所述。请修复它并重新发布完整的方法。
        • 我还需要一个答案,拜托。当您询问时,我再次添加了代码。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-10
        • 2012-06-30
        • 2010-12-23
        • 1970-01-01
        相关资源
        最近更新 更多