【问题标题】:Not able to UPDATE the SQlite3 table无法更新 SQlite3 表
【发布时间】:2013-11-04 21:44:43
【问题描述】:

我在更新创建的 sqlite 表时做了很多谷歌搜索,但我仍然无法在我的示例应用程序中更新我的表。谁能告诉我下面的代码有什么问题。直到 sqlite3_prepare_v2。一旦达到 if(sqlite3_prepare_v2(database, sql, -1, &statement, NULL)==SQLITE_OK) 条件,它就不会进入这个 if() 条件,任何人都可以告诉这里发生了什么?

const char *dbpath = [[self DBPath] UTF8String];
if(sqlite3_open(dbpath, &database) == SQLITE_OK)
{

    NSString *querySql=[NSString stringWithFormat:@"UPDATE Table1 SET AppEndTime = %@ WHERE AppID= %d",AppEndTime,appID];
    const char *sql=[querySql UTF8String];
    if(sqlite3_prepare_v2(database, sql, -1, &statement, NULL)==SQLITE_OK){
        sqlite3_bind_int(statement, 1, sessionID);
        sqlite3_bind_text(statement, 6, [sessionEndTime UTF8String], -1, SQLITE_TRANSIENT);

    }
}
char* errmsg;
sqlite3_exec(database, "COMMIT", NULL, NULL, &errmsg);

if(SQLITE_DONE != sqlite3_step(statement)){
    NSLog(@"Error while updating. %s", sqlite3_errmsg(database));
}
else{
   sqlite3_reset(statement);
}
sqlite3_finalize(statement);
sqlite3_close(database);

【问题讨论】:

  • 你没有任何日志信息吗?也许您必须在查询中引用 AppEndTime 的值。

标签: ios iphone sqlite


【解决方案1】:

请按照下面给出的代码替换您的代码。希望它能解决您的问题。

if (sqlite3_prepare_v2(database, sql, -1, &Statement1, NULL) == SQLITE_OK) {

        sqlite3_bind_text(Statement1, 1, [status UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(Statement1, 2, [messageID UTF8String], -1, NULL);

        int success = sqlite3_step(Statement1);
        if(success != SQLITE_ERROR)
        {
        //  NSLog(@"Success");
        }

        sqlite3_finalize(Statement1);
    }

【讨论】:

  • 感谢您的回复。但是您回答无法解决我的问题,您可以编辑我的代码以便我更好地理解
【解决方案2】:

这是我更新创建表的代码。看看吧,希望对你有帮助。

-(BOOL)updateMyTable{
BOOL isGood = YES;
@try {

    NSString *fName = [user_dic valueForKey:@"fName"];

    NSString *lName = [user_dic valueForKey:@"lName"];

    NSString *email_id = [user_dic valueForKey:@"email_id"];

    NSString *employee_id = [user_dic valueForKey:@"employee_id"];

    fName= [fName stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
    lName= [lName stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
    email_id= [email_id stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
    employee_id= [employee_id stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
    // NSString *autoId = [user_dic valueForKey:@"autoId"];

    NSString *sql = [NSString stringWithFormat:@"UPDATE tbl_profile SET fName = '%@', lName  = '%@', email_id  = '%@' ,employee_id = '%@' WHERE autoId = '%@'",fName,lName,email_id,employee_id,autoId];

    [database executeUpdate:sql,nil];
    if (MyDelegate.isLogging) {
        NSLog(@"edit user QUERY---- >>>>%@",sql);
        NSLog(@"edit user RESULT CODE ---- >>>>%d:", [database lastErrorCode]);
        NSLog(@"edit user RESULT ERROR MESSAGE ---- >>>>%@",[database lastErrorMessage]);
    }

}@catch (NSException *exception) {
    isGood = NO;
}
@finally {

}

return isGood;

【讨论】:

    【解决方案3】:

    请确保您的 Sqlite 文件在文档目录中,而不是在项目文件夹中。 当且仅当文件在文档目录中时,更新和插入才会起作用

    see this answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-23
      • 2012-09-04
      • 1970-01-01
      • 2021-05-24
      • 2010-09-24
      • 1970-01-01
      • 1970-01-01
      • 2010-09-21
      相关资源
      最近更新 更多