Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios SQLite未更新表-Objective-c中的记录_Ios_Objective C_Iphone_Sqlite - Fatal编程技术网

Ios SQLite未更新表-Objective-c中的记录

Ios SQLite未更新表-Objective-c中的记录,ios,objective-c,iphone,sqlite,Ios,Objective C,Iphone,Sqlite,我正在尝试更新表中的值。我正在执行下面的update查询来更新特定的记录。查询执行正常,但未更新记录。我已检查数据库是否在文档目录中,数据库打开是否成功。不知道哪里出错了。为什么表没有得到更新 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES ); NSString *path = [paths objectAtIndex:

我正在尝试更新表中的值。我正在执行下面的update查询来更新特定的记录。查询执行正常,但未更新记录。我已检查数据库是否在文档目录中,数据库打开是否成功。不知道哪里出错了。为什么表没有得到更新

        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 *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);
以下是您的代码的干净版本:

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)

仅供参考-您应该在
if(sqlite3\u prepare\u v2…)
块内调用
sqlite3\u步骤和
sqlite3\u finalize
。您应该在
if(sqlite3\u open…
块的末尾调用
sqlite3\u close
。您可能应该解释问题是什么,以及如何更正它。是的。我的索引错了。作物为绑定指数1,种子为指数2,宽度为指数3,id为指数4。同意。这是唯一的原因。作物为绑定指数1,种子为指数2,宽度为指数3,id为指数4。