ios - 在 ios 中的 sqlite 更新中出现错误

标签 ios iphone sqlite

我在sqlite更新中遇到错误,这里正确给出了查询,但值没有更新。这里我需要更新给定位置的描述值,下面我添加了我的代码,请帮助我

-(void) update:(NSString *)filePath withName:(NSString *)place description:(NSString*)description
{
    sqlite3* db = NULL;
    int rc=0;
     sqlite3_stmt* stmt =NULL;

rc = sqlite3_open_v2([filePath cStringUsingEncoding:NSUTF8StringEncoding], &db, SQLITE_OPEN_READWRITE , NULL);
if (SQLITE_OK != rc)
{
    sqlite3_close(db);
    NSLog(@"Failed to open db connection");
}
else
{
    NSString * query  = [NSString stringWithFormat:@"UPDATE  places SET description=%@ WHERE place=%@",description,place];

    sqlite3_bind_text(stmt, 1, [place UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(stmt, 2, [description UTF8String], -1, SQLITE_TRANSIENT);

    NSLog(@"query %@",query);
    char * errMsg;
    rc = sqlite3_exec(db, [query UTF8String] ,NULL,&stmt,&errMsg);
    if(SQLITE_OK != rc)
    {
        NSLog(@"Failed to insert record  rc:%d, msg=%s",rc,errMsg);
    }
    else{
    NSLog(@"Sucessfully updated");
    }
    sqlite3_finalize(stmt);
    sqlite3_close(db);
 }
}

创建表

-(int) createTable:(NSString*) filePath
{
    sqlite3* db = NULL;
    int rc=0;
NSLog(@"create");
rc = sqlite3_open_v2([filePath cStringUsingEncoding:NSUTF8StringEncoding], &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
if (SQLITE_OK != rc)
{
    sqlite3_close(db);
    NSLog(@"Failed to open db connection");
}
else
{
    char * query ="CREATE TABLE IF NOT EXISTS places ( id INTEGER PRIMARY KEY AUTOINCREMENT,place TEXT ,locationname TEXT,time TEXT,description TEXT,notifyTime TEXT,radius TEXT,lat DOUBLE,lon DOUBLE ,notify TEXT,selection INTEGER)";
    char * errMsg;
    rc = sqlite3_exec(db, query,NULL,NULL,&errMsg);

    if(SQLITE_OK != rc)
    {
        NSLog(@"Failed to create table rc:%d, msg=%s",rc,errMsg);
    }
    else{
        NSLog(@"Sucessfully Created ");
    }

    sqlite3_close(db);
}
return rc;

最佳答案

首先打开数据库连接。 然后写下面的代码。

               NSString * query  = [NSString stringWithFormat:@"UPDATE  places SET description=%@ WHERE place=%@",description,place];
               SQL=[NSString stringWithString:query];
                sqlite3_stmt *insert_statement=nil;

                if (sqlite3_prepare_v2(database, [SQL UTF8String], -1, &insert_statement, NULL) != SQLITE_OK) {
                    //NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
                    NSLog(@"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
                }

                int result=sqlite3_step(insert_statement);
                sqlite3_finalize(insert_statement);

然后关闭数据库连接

关于ios - 在 ios 中的 sqlite 更新中出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27687717/

相关文章:

ios - 在 iOS 上,我的应用程序如何获取手机在后台时收到的通知数据?

ios - 通过 AVPlayer 播放实时 URL 流

iphone - 解密时 OSStatus -9809

SWIFT 在 SQL 中使用 INT

iphone - 另一个 View 中的弹出 View

ios - 未为协议(protocol)调用委托(delegate) respondsToSelector

ios - 如何让某些特定客户可以使用我的 iPhone 应用程序?

c++ - 如何将ios设备联系人与qt app同步?

android - 为什么我的 Assets 文件夹没有安装在模拟器上?

python - 使用 Python 将 CSV 解析为 API 的数据库?