更新iPhone SQLite DB

时间:2010-10-21 05:42:10

标签: iphone sql sqlite sql-update

我想使用SQLite更新查询来更新iPhone中的数据库。

有人能为我提供代码怎么做?

2 个答案:

答案 0 :(得分:1)

鉴于已经开放的数据库:

static sqlite3_stmt *update_progress_statement = nil;

- (void) setProgress:(int)newProgress forKey:(int)key {
    if (update_progress_statement == nil) {
        const char *sql = "UPDATE progress SET value = ? WHERE key = ?";
        if (sqlite3_prepare_v2(database, sql, -1, &update_progress_statement, NULL) != SQLITE_OK) {
            NSLog(@"Error: failed to prepare update_progress_statement with message '%s'.",sqlite3_errmsg(database));
            return;
        }
    }
    sqlite3_bind_int(update_progress_statement, 1, newProgress);
    sqlite3_bind_int(update_progress_statement, 2, key);
    int success = sqlite3_step(update_progress_statement);
    if (success == SQLITE_ERROR){
        NSLog(@"Error: failed to update update_progress_statement with message '%s'.", sqlite3_errmsg(database));
    }
    sqlite3_reset(update_progress_statement);
}

答案 1 :(得分:0)

+ (BOOL)UpDateGetAvailableContentObjects:(NSMutableDictionary*)availableReportsDict

{   
    //The following two lines of code are sure to be called because this method has to be called.  
    //So these two lines will set the static database variable for use in future as well in add, delete, update functions.

htmlReportAppDelegate *appDelegate = (htmlReportAppDelegate *)
[[UIApplication sharedApplication] delegate];

database = appDelegate.database;

const char *sqlStatement = "insert into 

GetAvailableContentObjectsTable(contentID,id,lastUpdate,name,typeid,subpages) Values(?,?,?,?,?,?)";

int success;

if(sqlite3_prepare_v2(database, sqlStatement, -1, &get_statement, NULL) == SQLITE_OK) 

{


sqlite3_bind_text(get_statement, 1, [[availableReportsDict valueForKey:@"pages"] UTF8String], -1, SQLITE_TRANSIENT);

sqlite3_bind_text(get_statement, 2, [[availableReportsDict valueForKey:@"id"] UTF8String], -1, SQLITE_TRANSIENT);

sqlite3_bind_text(get_statement, 3, [[availableReportsDict valueForKey:@"lastUpdate"] UTF8String], -1, SQLITE_TRANSIENT);

sqlite3_bind_text(get_statement, 4, [[availableReportsDict valueForKey:@"name"] UTF8String], -1, SQLITE_TRANSIENT);

sqlite3_bind_int(get_statement, 5, [[availableReportsDict valueForKey:@"typeid"]intValue]);

sqlite3_bind_int(get_statement, 6, [[availableReportsDict valueForKey:@"subpages"]intValue]);


success = sqlite3_step(get_statement);


if (success == SQLITE_ERROR) {

NSAssert1(0, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(database));

}

}

else {

NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));

}

// Release the compiled statement from memory

sqlite3_finalize(get_statement);

get_statement = nil;

return success;

}