【问题标题】:SQLite is not updating the record in Table - Objective-cSQLite 没有更新表中的记录 - Objective-c
【发布时间】:2017-10-23 20:04:52
【问题描述】:

我正在尝试更新表中的值。我正在执行以下更新查询以更新特定记录。查询执行良好,但未更新记录。我检查了数据库在文档目录中,并且数据库打开成功。不知道哪里出错了。为什么表没有更新。

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES );
        NSString *path = [paths objectAtIndex:0];
        NSString *fullPath = [path stringByAppendingPathComponent:@"agpla.sqlite"];


        sqlite3_stmt *updateStmt;
        const char *dbpath = [fullPath UTF8String];
        if(sqlite3_open(dbpath, &database) == SQLITE_OK)
        {
            const char *sql = "UPDATE favorites SET Crop = ?, Seeds = ?, Width = ? Where id=?";
            if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL)==SQLITE_OK){
                sqlite3_bind_text(updateStmt, 4, [titleLabel.text UTF8String], -1, SQLITE_TRANSIENT);
               // sqlite3_bind_text(updateStmt, 1, [seedsField.text UTF8String], -1, SQLITE_TRANSIENT);
                //sqlite3_bind_text(updateStmt, 2, [rowField.text UTF8String], -1, SQLITE_TRANSIENT);
                 sqlite3_bind_int(updateStmt, 1, [seedsField.text intValue]);
                 sqlite3_bind_int(updateStmt, 2, [rowField.text intValue]);
               sqlite3_bind_int(updateStmt, 3, [appDel.idToDelete intValue]);
            }
        }
        if(sqlite3_step(updateStmt) != SQLITE_DONE) {
            NSLog(@"Problems updating entry in reminder");
        }

        /* Finished */
        sqlite3_finalize(updateStmt);

【问题讨论】:

  • 仅供参考 - 您应该在 if (sqlite3_prepare_v2...) 块内调用 sqlite3_stepsqlite3_finalize。你应该在if (sqlite3_open...) 块的末尾调用sqlite3_close

标签: ios objective-c iphone sqlite


【解决方案1】:

这里有几个问题。但主要似乎是您在where 子句中绑定到id 的值与任何现有记录都不匹配。请记住,Crop 是绑定索引 1,Seeds 是索引 2,Width 是索引 3,id 是索引 4。

这是您的代码的干净版本:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES );
NSString *path = [paths objectAtIndex:0];
NSString *fullPath = [path stringByAppendingPathComponent:@"agpla.sqlite"];


sqlite3_stmt *updateStmt;
const char *dbpath = [fullPath UTF8String];
if(sqlite3_open(dbpath, &database) == SQLITE_OK)
{
    const char *sql = "UPDATE favorites SET Crop = ?, Seeds = ?, Width = ? Where id=?";
    if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL)==SQLITE_OK){
        sqlite3_bind_text(updateStmt, 1, [titleLabel.text UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_int(updateStmt, 2, [seedsField.text intValue]);
        sqlite3_bind_int(updateStmt, 3, [rowField.text intValue]);
        sqlite3_bind_int(updateStmt, 4, [appDel.idToDelete intValue]);

        if(sqlite3_step(updateStmt) != SQLITE_DONE) {
            NSLog(@"Problems updating entry in reminder");
        }

        /* Finished */
        sqlite3_finalize(updateStmt);
    }

    sqlite3_close(database);
}

【讨论】:

  • 同意。那是唯一的原因。 Crop 为 bind index 1,Seeds 为 index 2,Width 为 index 3,id 为 index 4。
【解决方案2】:

好的,我发现它现在可以工作了 :) 我的索引是错误的。 Crop是bind index 1,Seeds是index 2,Width是index 3,id是index 4。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,        NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"agpla.sqlite"];
        //NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Accounts.sqlite"];
       // sqlite3 *database;
        sqlite3_stmt *updateStmt;
        if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK)
        {
            const char *sql = "update favorites Set Seeds = ?, Width = ?  Where ID = ?";
            if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) != SQLITE_OK)
                NSLog(@"Error while creating update statement. %s", sqlite3_errmsg(database));
        }
        sqlite3_bind_int(updateStmt, 1, [seedsField.text intValue]);
        sqlite3_bind_int(updateStmt, 2, [rowField.text intValue]);
        sqlite3_bind_int(updateStmt, 3 , [appDel.idToDelete intValue]);


        char* errmsg;
        sqlite3_exec(database, "COMMIT", NULL, NULL, &errmsg);

        if(SQLITE_DONE != sqlite3_step(updateStmt))
            NSLog(@"Error while updating. %s", sqlite3_errmsg(database));
        sqlite3_finalize(updateStmt);


        sqlite3_close(database);

【讨论】:

  • 您应该解释一下问题是什么,以及您是如何纠正的。
  • 是的。我的索引是错误的。 Crop 为 bind index 1,Seeds 为 index 2,Width 为 index 3,id 为 index 4。
【解决方案3】:

在 Swift 3.1 中

   var paths: [Any] = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
var documentsPath: String? = (paths[0] as? String)
var filePath: String = URL(fileURLWithPath: documentsPath!).appendingPathComponent("agpla.sqlite").absoluteString
    //NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Accounts.sqlite"];
    // sqlite3 *database;
var updateStmt: sqlite3_stmt?
if sqlite3_open(filePath.utf8, database) == SQLITE_OK {
    let sql = "update favorites Set Seeds = ?, Width = ?  Where ID = ?"
    if sqlite3_prepare_v2(database, sql, -1, updateStmt, nil) != SQLITE_OK {
        print("Error while creating update statement. \(sqlite3_errmsg(database))")
    }
}
sqlite3_bind_int(updateStmt, 1, CInt(seedsField.text))
sqlite3_bind_int(updateStmt, 2, CInt(rowField.text))
sqlite3_bind_int(updateStmt, 3, CInt(appDel.idToDelete))
var errmsg = [CChar]()
sqlite3_exec(database, "COMMIT", nil, nil, errmsg)
if SQLITE_DONE != sqlite3_step(updateStmt) {
    print("Error while updating. \(sqlite3_errmsg(database))")
}
sqlite3_finalize(updateStmt)
sqlite3_close(database)

【讨论】:

    猜你喜欢
    • 2021-06-08
    • 2011-04-10
    • 1970-01-01
    • 2011-02-19
    • 2021-11-15
    • 2023-03-06
    • 2018-06-09
    • 2018-05-29
    • 1970-01-01
    相关资源
    最近更新 更多