尝试使用sqlite将数据插入数据库时​​出错

时间:2011-04-11 08:51:32

标签: iphone objective-c xcode

库例程不按顺序
我可以知道为什么会出现这个错误(这个错误出现在插入函数中)

-(void) checkAndCreateDatabase{
    // Check if the SQL database has already been saved to the users phone, if not then copy it over
    BOOL success;

    // Create a FileManager object, we will use this to check the status
    // of the database and to copy it over if required
    NSFileManager *fileManager = [NSFileManager defaultManager];

    // Check if the database has already been created in the users filesystem
    success = [fileManager fileExistsAtPath:databasePath];

    // If the database already exists then return without doing anything
    if(success) return;

    // If not then proceed to copy the database from the application to the users filesystem

    // Get the path to the database in the application package
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];

    // Copy the database from the package to the users filesystem
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];

    [fileManager release];
}

- (void)readAnimalsFromDatabase {     //设置数据库对象

// Init the animals Array
animals = [[NSMutableArray alloc] init];
// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
    // Setup the SQL Statement and compile it for faster access


    const char *sqlStatement = "select * from sq";
    sqlite3_stmt *compiledStatement;
    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
        // Loop through the results and add them to the feeds array
            NSLog(@"working");
        while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
            // Read the data from the result row
            NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];

            // Create a new animal object with the data from the database
            anim *animal = [[anim alloc] initWithName:aName];

            // Add the animal object to the animals Array
            [animals addObject:animal];

            [animal release];
        }
    }
    // Release the compiled statement from memory
    sqlite3_finalize(compiledStatement);

}
sqlite3_close(database);

} //按钮按下事件

从另一个文件调用此函数
-(void)insert:str  
{  
const char *sql = "insert into sq(name) Values ?";  

sqlite3_stmt *compiledStatement;  

if(sqlite3_prepare_v2(database, sql, -1, &compiledStatement, NULL) != SQLITE_OK)  

NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));     
sqlite3_bind_text(compiledStatement, 1, [str UTF8String], -1, SQLITE_TRANSIENT);  

    if(SQLITE_DONE != sqlite3_step(compiledStatement))
        NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
    else
    sqlite3_reset(compiledStatement) ;
}

1 个答案:

答案 0 :(得分:2)

该错误消息映射到SQLITE_MISUSE(源代码可在http://www.sqlite.org获得)。

有关从多个线程使用sqlite3 *数据库句柄的限制,请参阅http://www.sqlite.org/faq.html#q6。实际上,您可以跨线程重用数据库句柄和语句,但在另一个线程启动之前必须完全访问数据库(即重叠访问不安全)。这听起来像是你发生的事情,并且与SQLITE_MISUSE错误代码一致。

更多请参阅以下SO帖子..

How to properly call SQLite functions from background thread on iPhone?