在ios中的sqlite Update中获取错误

时间:2014-12-29 11:10:45

标签: 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;

}

3 个答案:

答案 0 :(得分:1)

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

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

然后关闭数据库连接

答案 1 :(得分:0)

试试这个...检查db是否打开并更改此代码...

 -(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  *strMQueryupdate=[NSString stringWithFormat:@"update br set TOTAL='%@',QUTY='%@' WHERE PID='%@'",[@(total )stringValue],[@(lblQtn )stringValue],produdId];
             */

          NSString * query  = [NSString stringWithFormat:@"UPDATE  places SET description='%@' WHERE place='%@'",description,place];
            const char *sql = [query UTF8String];

            if (sqlite3_prepare_v2(db, sql, -1, & stmt, NULL) != SQLITE_OK)
            {
                NSLog(@"update fails");
            }
            else
            {
                sqlite3_bind_text(stmt, 1, [place UTF8String], -1, SQLITE_TRANSIENT);
                sqlite3_bind_text(stmt, 2, [description UTF8String], -1, SQLITE_TRANSIENT);

                int success = sqlite3_step(stmt);
                sqlite3_reset(stmt);
                if (success == SQLITE_ERROR)
                {

                }
                else
                {
                    NSLog(@"update success");

                }
                sqlite3_finalize(update_statement);
                sqlite3_close(db);
            }
    }

答案 2 :(得分:0)

您正在错误地使用预准备语句。您对字符串中的值进行硬编码(一个坏主意),然后设置占位符值 - 这没有任何意义,因为SQL中没有占位符。尝试类似:

NSString * query  = @"UPDATE  places SET description= ? WHERE place= ?";

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

您不希望将值直接包含在查询字符串中的原因:想象某人传入的值为“London”; drop table places;“。 (不确定这是否有用,但它应该让你知道用户可以达到的那种恶作剧。)