繁体   English   中英

SQLite不更新表中的记录-Objective-C

[英]SQLite is not updating the record in Table - Objective-c

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

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

好的,我发现它现在可以工作了:)我的索引错误。 作物是绑定索引1,种子是索引2,宽度是索引3,id是索引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);

这里有几个问题。 但是主要的似乎是您绑定到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);
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM