Sqlite数据库锁定错误 - iOS

时间:2016-02-25 09:09:04

标签: ios objective-c sqlite

我使用sqlite来使用insert并更新表视图中的数据。我只能在下次尝试显示数据库锁定时更新数据一次。即使关闭数据库,请帮忙。以下是代码。

-(void)saveUserCredential: (NSString *)email :(NSString *)userName :(NSString *)loginTime :(NSString *)source
{
NSDateFormatter *dateformate=[[NSDateFormatter alloc]init];
[dateformate setDateFormat:@"yyyy-MM-dd HH:mm"]; // Date formater
NSString *todayDate = [dateformate stringFromDate:[NSDate date]];

const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
    NSString  * query = @"SELECT * from users";
    int rc =sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, NULL);
    if(rc == SQLITE_OK)
    {
        if(sqlite3_step(statement) == SQLITE_ROW)
        {

              NSString *updateSQL = [NSString stringWithFormat:@"update users set email = '%@', username = '%@', source = '%@', created = '%@' where id = '%d'",email, userName, source, todayDate,1];

            const char *update_stmt = [updateSQL UTF8String];
            sqlite3_prepare_v2(database, update_stmt, -1, &statement, NULL );
            //sqlite3_bind_int(statement, 1, 1);
            if (sqlite3_step(statement) == SQLITE_DONE)
            {
                NSLog(@"successfully updated");
            }
            else{

                NSLog(@"Error while updating. '%s'", sqlite3_errmsg(database));
            }

        }
        else
        {
            NSString *insertSQL = [NSString stringWithFormat:@"insert into users (email,username,source,created) values('%@','%@','%@','%@')",email,userName,source,todayDate];

            NSLog(@"INS SQL: %@", insertSQL);
            const char *insert_stmt = [insertSQL UTF8String];
            sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL);
            if (sqlite3_step(statement) == SQLITE_DONE)
            {
                NSLog(@"INSERTED");
            }
            else
            {
                NSLog(@"NOT INSERTED");
            }
            NSLog(@"hello ");

        }
        sqlite3_finalize(statement);
        sqlite3_close(database);

    }

}

}

1 个答案:

答案 0 :(得分:2)

您需要致电

sqlite3_finalize(statement);

每次成功sqlite_prepare_v2来电。它应该是平衡的。对每个查询也使用不同的sqlite3_stmt

所以你的代码需要改变如下:

const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
    NSString  * query = @"SELECT * from users";
    int rc =sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, NULL);
    if(rc == SQLITE_OK)
    {
        if(sqlite3_step(statement) == SQLITE_ROW)
        {

              NSString *updateSQL = [NSString stringWithFormat:@"update users set email = '%@', username = '%@', source = '%@', created = '%@' where id = '%d'",email, userName, source, todayDate,1];

            const char *update_stmt = [updateSQL UTF8String];
            sqlite3_stmt *upStmt;
            sqlite3_prepare_v2(database, update_stmt, -1, &upStmt, NULL );
            //sqlite3_bind_int(upStmt, 1, 1);
            if (sqlite3_step(upStmt) == SQLITE_DONE)
            {
                NSLog(@"successfully updated");
            }
            else
            {

                NSLog(@"Error while updating. '%s'", sqlite3_errmsg(database));
            }
            sqlite3_finalize(upStmt);
        }
        else
        {
            NSString *insertSQL = [NSString stringWithFormat:@"insert into users (email,username,source,created) values('%@','%@','%@','%@')",email,userName,source,todayDate];

            NSLog(@"INS SQL: %@", insertSQL);
            const char *insert_stmt = [insertSQL UTF8String];
            sqlite3_stmt *inStmt;
            sqlite3_prepare_v2(database, insert_stmt,-1, &inStmt, NULL);
            if (sqlite3_step(inStmt) == SQLITE_DONE)
            {
                NSLog(@"INSERTED");
            }
            else
            {
                NSLog(@"NOT INSERTED");
            }
            NSLog(@"hello ");
           sqlite3_finalize(inStmt);

        }
        sqlite3_finalize(statement);
        sqlite3_close(database);

    }

}