簡體   English   中英

在ios中的sqlite更新中獲取錯誤

[英]getting error in sqlite Update in ios

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

然后關閉數據庫連接

試試這個...檢查數據庫是否已打開並更改此代碼...

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

您使用的准備語句不正確。 您將值硬編碼為字符串(不好的主意),然后設置占位符值-這沒有任何意義,因為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);

您不想直接在查詢字符串中包含值的原因:想象有人在為“倫敦”,放置表格位置;等位置傳遞值。 (不知道這是否行得通,但是它應該使您對用戶可能會冒犯的惡作劇有所了解。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM