更新 iPhone SQLite 数据库

发布于 2024-09-28 13:34:12 字数 66 浏览 2 评论 0原文

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

谁能给我提供如何做到这一点的代码?

I want to use SQLite update query to update the database in iPhone.

Could anyone provide me with the code how to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

箹锭⒈辈孓 2024-10-05 13:34:12

给定一个已经打开的数据库:

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

Given an already open database:

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);
}
旧话新听 2024-10-05 13:34:12
+ (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;

}
+ (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;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文