SQLite3更新无法正常工作

时间:2011-06-28 23:19:06

标签: iphone ios sqlite

我正在尝试对我的sqlite3表执行更新命令。但它不起作用:

+(void)updateContact:(Contact *)c withOriginalFirst:(NSString *)originalFirst originalLast:(NSString *)originalLast originalBriefDescription:(NSString *)originalBriefDescription {

    sqlite3 *database;
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {

        NSString *cmd = [NSString stringWithFormat:@"update contacts set first='%@', last='%@', briefDescription='%@' where first='%@' and last='%@' and briefDescription='%@';",
                         [c first],[c last],[c briefDescription],originalFirst,originalLast,originalBriefDescription];
        const char * sql = [cmd UTF8String];
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database, sql, -1, &compiledStatement, NULL) == SQLITE_OK) {
            NSLog(@"updateContact SUCCESS - executed command %@",cmd);
        }
        else {
            NSLog(@"updateContact FAILED - failed to execute command %@",cmd);
        }

        sqlite3_finalize(compiledStatement);

    }
    else {
        NSLog(@"pdateContact FAILED - failed to open database");
    }

    sqlite3_close(database);

    NSLog(@"After update, contacts = %@",[SQLMaster getContactsFromDatabase]);

}

我看到“updateContact SUCCESS - 执行命令......”打印出来。但该表未更新。我做错了什么?

3 个答案:

答案 0 :(得分:2)

一些事情:

您的databasePath指向哪里?确保它指向包中的文档路径,而不是直接指向项目中的外部数据库。另外,请确保正确放置步骤语句。

+(void)updateContact:(Contact *)c withOriginalFirst:(NSString *)originalFirst originalLast:(NSString *)originalLast originalBriefDescription:(NSString *)originalBriefDescription {

sqlite3 *database;
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {

    NSString *cmd = [NSString stringWithFormat:@"update contacts set first='%@', last='%@', briefDescription='%@' where first='%@' and last='%@' and briefDescription='%@';",
                     [c first],[c last],[c briefDescription],originalFirst,originalLast,originalBriefDescription];
    const char * sql = [cmd UTF8String];
    sqlite3_stmt *compiledStatement;
    if(sqlite3_prepare_v2(database, sql, -1, &compiledStatement, NULL) == SQLITE_OK) {
        sqlite3_step(compiledStatement); // Here is the added step.
        NSLog(@"updateContact SUCCESS - executed command %@",cmd);
    }
    else {
        NSLog(@"updateContact FAILED - failed to execute command %@",cmd);
    }

    sqlite3_finalize(compiledStatement);

}
else {
    NSLog(@"pdateContact FAILED - failed to open database");
}

    sqlite3_close(database);
    NSLog(@"After update, contacts = %@",[SQLMaster getContactsFromDatabase]);

}

另外,我从[SQLMaster getContactsFromDatabase]行看到您正在调用其他数据库方法。确保在所有这些数据库调用上都达到sqlite3_finalize,否则数据库可能不会释放繁忙的处理程序。

答案 1 :(得分:2)

这可能对你有帮助......

  +(void)updateContact:(Contact *)c withOriginalFirst:(NSString *)originalFirst originalLast:(NSString *)originalLast originalBriefDescription:(NSString *)originalBriefDescription 

    {
    if(sqlite3_open([databasePath UTF8String],&myDatabase)==SQLITE_OK)
{
    sqlite3_stmt *compiledStmt;
    NSString  *sqlStmt=[NSString stringWithFormat:@"UPDATE contacts SET first =?, last =? , briefDescription =?, briefDescription =? WHERE first=?,last=?,briefDescription=?;"];

    if(sqlite3_prepare_v2(myDatabase, [sqlStmt UTF8String],-1,&compiledStmt, NULL)==SQLITE_OK)
    {

         NSLog(@"updateding......cycle");
    sqlite3_bind_text(compiledStmt,1, [[c first] UTF8String],-1,SQLITE_TRANSIENT);
        sqlite3_bind_text(compiledStmt,2, [[c last] UTF8String],-1,SQLITE_TRANSIENT);

     sqlite3_bind_text(compiledStmt,3, [[c briefDescription] UTF8String],-1,SQLITE_TRANSIENT);  

   sqlite3_bind_text(compiledStmt,4, [originalFirst UTF8String],-1,SQLITE_TRANSIENT);

   sqlite3_bind_text(compiledStmt,5, [originalLast UTF8String],-1,SQLITE_TRANSIENT);

    sqlite3_bind_text(compiledStmt,6, [originalBriefDescription UTF8String],-1,SQLITE_TRANSIENT);
    }
    sqlite3_step(compiledStmt);
    sqlite3_close(database);
}



  }

答案 2 :(得分:1)

您准备了该声明但未执行该声明。尝试int sqlite3_step(sqlite3_stmt *);准备好之后。