gpt4 book ai didi

objective-c - 我的更新语句不起作用

转载 作者:行者123 更新时间:2023-11-29 11:15:12 27 4
gpt4 key购买 nike

我尝试像这样更新表中的条目:

NSFileManager *fileMgr=[NSFileManager defaultManager];
NSString *dbPath=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"quizDB.sqlite"];

if ([fileMgr fileExistsAtPath:dbPath]) {
NSLog(@"DB does exist!");//displayed
}
const char *dbpath = [dbPath UTF8String];
sqlite3_stmt *updateStmt;
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSLog(@"%i",id_quiz);//displayed, in a test case it display 1 which is a quiz ID in the table
NSString *querySql=[NSString stringWithFormat:@"Update quiz set etat=1 where id=\"%i\"",id_quiz];
const char*sql=[querySql UTF8String];

sqlite3_prepare_v2(contactDB, sql, -1, &updateStmt, NULL);

if(SQLITE_DONE != sqlite3_step(updateStmt))
{
NSAssert1(0, @"Error while updating. '%s'", sqlite3_errmsg(contactDB));
}
else{

sqlite3_reset(updateStmt);
NSLog(@"Update done successfully!");//this is also displayed, i guess everything is ok then and the entry is updated
}

sqlite3_finalize(updateStmt);
sqlite3_close(contactDB);
}

在日志中,我收到消息:更新成功完成! 所以我假设一切正常,但是当我在 Mozilla Firefox 的 SQLite Manager 中检查数据库时,条目没有更新。我错过了什么吗?感谢帮助

最佳答案

首先将您的数据库从资源复制到文档目录:

- (void) checkAndCreateDatabase
{
NSString *database_Name =@"quizDB.sqlite"
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *database_Path = [documentsDir stringByAppendingPathComponent:database_Name];

NSFileManager *fileManager = [NSFileManager defaultManager];

if(![fileManager fileExistsAtPath:database_Path])
{
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:database_Name];

[fileManager removeItemAtPath:database_Path error:nil];
[fileManager copyItemAtPath:databasePathFromApp toPath:database_Path error:nil];
}
else
{
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:database_Name];
[fileManager copyItemAtPath:databasePathFromApp toPath:database_Path error:nil];
}

}

更新数据库后:

 - (void) UpdateDatabase
{
sqlite3 *contactDB;
sqlite3_stmt *updateStmt;
NSString *database_Name =@"quizDB.sqlite"

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *dbpath = [documentsDir stringByAppendingPathComponent:database_Name];

if(sqlite3_open([dbpath UTF8String], &contactDB) == SQLITE_OK)
{
NSString *querySql=[NSString stringWithFormat:@"Update quiz set etat=1 where id=\"%i\"",id_quiz];
const char*sql=[querySql UTF8String];
if(sqlite3_prepare_v2(contactDB,sql, -1, &updateStmt, NULL) == SQLITE_OK)
{
if(SQLITE_DONE != sqlite3_step(updateStmt))
{
NSAssert1(0, @"Error while updating. '%s'", sqlite3_errmsg(contactDB));
}
else{
sqlite3_reset(updateStmt);
NSLog(@"Update done successfully!");
}
}



sqlite3_finalize(updateStmt);
}
sqlite3_close(contactDB);

}

并检查你的数据库,它保存在 documentdir 而不是资源中

关于objective-c - 我的更新语句不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9767174/

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