簡體   English   中英

sqlite3更新在iOS中不起作用

[英]sqlite3 update not working in ios

我正在嘗試更新sqlite數據庫。 這是我正在使用的代碼

if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
    {

        const char * sql;
        sql = "update tms  set name = ?,place=?,stars=?  where id=?";

        sqlite3_stmt *selectStatement;

        //prepare the select statement
        int returnValue = sqlite3_prepare_v2(database, sql, -1, &selectStatement, NULL);
        if(returnValue == SQLITE_OK)
        {
            sqlite3_bind_text(selectStatement, 1,[[payloadDict  valueForKey:@"userName"] UTF8String] , [[payloadDict  valueForKey:@"userName"] length],SQLITE_STATIC);
            sqlite3_bind_text(selectStatement, 2,[[payloadDict  valueForKey:@"locName"] UTF8String], [[payloadDict  valueForKey:@"locName"] length],SQLITE_STATIC);
            sqlite3_bind_int(selectStatement, 3, [[payloadDict  valueForKey:@"starCount"] integerValue]);
            sqlite3_bind_int(selectStatement, 4, [[payloadDict  valueForKey:@"rowid"] integerValue]);
            int success = sqlite3_step(selectStatement);
            if(success == SQLITE_DONE)
            {
                isExist = TRUE;
            }
            else {
                //NSAssert1(0,@"Error: Failed to Update %s",sqlite3_errmsg(database));
            }
    }

執行sqlite3_step時,我獲得的成功值為101。 但是不會使用新值更新數據庫。 如何正確執行此操作? 謝謝

我同意@ott關於確保數據庫位於Documents目錄中的出色建議(盡管我會認為那會給您帶來錯誤)。

我還要仔細檢查[[payloadDict valueForKey:@"rowid"] integerValue]返回的值,以確保它與表中現有行之一的id列中的值匹配。 如果不匹配,即使未進行任何更新, sqlite3_step都將返回SQLITE_DONE

還要注意,您可能還想確保id值存儲為數字值,而不是文本字符串,因為sqlite對於讓您將值存儲為首次插入數據時最初指定的任何數據類型都相當寬松該表已定義),並且我不確定如果數據最初存儲為文本值,那么尋找數字匹配的WHERE子句是否會成功。 如果您使用了id列定義(例如id INTEGER PRIMARY KEY AUTOINCREMENT ,則系統會自動為您定義值,那么這不是問題,但是如果您手動填充了id列,則可能需要仔細檢查。 (通常,在將字符串即時解釋為數字方面,它做得很好,但是有些奇怪的情況是有問題的:例如,如果您在數字字段中存儲的字符串值為“ 5,127”,則稍后再嘗試要檢索其數字值,sqlite將不知道在文本值“ 5,127”中用逗號做什么,並且會將數字值解釋為5,而不是5127。)

暫無
暫無

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

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