gpt4 book ai didi

database - SQLite3 : database not being saved

转载 作者:搜寻专家 更新时间:2023-10-30 21:37:58 28 4
gpt4 key购买 nike

我正在创建一个 Mac 应用程序,它将一些数据保存到 SQLite 数据库中。问题是:当我关闭数据库并再次打开时,数据不存在了。这是我的代码:

NSString *sql = [NSString stringWithFormat:@"INSERT INTO people(name, artist, id, album, location, cover) VALUES('%@', '%@', %d, '%@', '%@', '%@');", name, artist, ID, album, path, cover];

sqlite3 *database = NULL;
sqlite3_open([[[NSBundle mainBundle] pathForResource:@"db" ofType:@"database"]
cStringUsingEncoding:NSUTF8StringEncoding], &database);

sqlite3_stmt *statement;
sqlite3_prepare_v2(database, "CREATE TABLE IF NOT EXISTS people(name TEXT, id INT, artist TEXT, album TEXT, cover TEXT, location TEXT);", -1, &statement, NULL);
sqlite3_step(statement);
sqlite3_finalize(statement);
sqlite3_prepare_v2(database, [sql cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL);
sqlite3_step(statement);
sqlite3_finalize(statement);

statement = NULL;
sqlite3_close(database);

最佳答案

您可以使用此代码来检查您的插入:

sqlite3_stmt *stmt;
char *errmsg;
const char *sql = [isql cStringUsingEncoding:NSUTF8StringEncoding];

if (sqlite3_prepare_v2(database, sql, -1, &stmt, NULL) != SQLITE_OK) {
NSLog(@"SQL Warning: Erro ao executar a instrução '%s'.", sqlite3_errmsg(database));
sqlite3_reset(stmt);
sqlite3_finalize(stmt);
return;
}

if(!sqlite3_exec(database, sql, nil, &stmt, &errmsg) == SQLITE_OK) NSLog(@"SQL Warning: '%s'.", sqlite3_errmsg(database));

sqlite3_reset(stmt);
sqlite3_finalize(stmt);

关于database - SQLite3 : database not being saved,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6690082/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com