xcode sqlite 数据库更新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7169966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:48:09  来源:igfitidea点击:

sqlite database update

objective-cxcodesqlite

提问by Hadi

I am using the following code for updating an entry in my table in sqlite. not interferring with the other columns just have to edit the balance one. But its giving me no change in the database.Can someone help me out here..

我正在使用以下代码更新我的 sqlite 表中的条目。不干扰其他列只需要编辑平衡一列。但它给我的数据库没有任何变化。有人可以帮我在这里..

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"Accounts.sqlite"];

sqlite3 *database;
sqlite3_stmt *updateStmt;
if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK)
 {
    const char *sql = "update Account Set currentBalance = ? Where ID = ?";
    if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) != SQLITE_OK)
        NSAssert1(0, @"Error while creating update statement. '%s'", sqlite3_errmsg(database));
}
sqlite3_bind_int(updateStmt, 1, 1000);

sqlite3_bind_int(updateStmt, 2 , 1);
sqlite3_finalize(updateStmt);


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


sqlite3_close(database);

回答by Hadi

DO this...

做这个...

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"Accounts.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 Account Set currentBalance = ? 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, 1000);

sqlite3_bind_int(updateStmt, 2 , 1);


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