• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - Sqlite 数据库锁定错误 - iOS

[复制链接]
菜鸟教程小白 发表于 2022-12-12 18:40:43 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在使用 sqlite 在表格 View 中使用插入和更新数据。我只能更新一次数据,下次尝试显示数据库已锁定。即使正在关闭数据库,请帮忙。下面是代码。

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

    }

}

}



Best Answer-推荐答案


你需要打电话

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

    }

}

关于ios - Sqlite 数据库锁定错误 - iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35622772/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap