简体   繁体   中英

SQLite is not updating the record in Table - Objective-c

I'm trying to update the value in table. I'm executing the following query of update to update particular record. Query is executing fine but its not updating the record. I have checked database is in document directory and database opening is successfull. Don't know where is the error. why table is not getting updated.

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

Ok I found it working now :) My indexing was wrong. Crop is bind index 1, Seeds is index 2, Width is index 3, and id is 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);

There are several issues here. But the primary seems to be that the value you bind to the id in the where clause doesn't match any existing records. Keep in mind that Crop is bind index 1, Seeds is index 2, Width is index 3, and id is index 4.

Here's a clean version of your code:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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