我试图更新表格中的值。我正在执行以下更新查询以更新特定记录。查询执行正常,但不更新记录。我已检查数据库是否在文档目录中,数据库打开是否成功。不知道错误在哪里。为什么表没有得到更新。
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);
答案 0 :(得分:0)
好的我发现它现在正常工作:) 我的索引错了。 裁剪是绑定索引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);
答案 1 :(得分:0)
这里有几个问题。但主要似乎是您绑定到id
子句中的where
的值与任何现有记录都不匹配。请注意,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);
}
答案 2 :(得分:-1)
在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)