我正在尝试更新SQLite数据库,但我遇到了麻烦。每次我点击按钮更新我都会收到此错误:
无法提交 - 没有事务处于活动状态
我看不出我的代码有什么问题,所以我不知道如何解决这个问题。
-(BOOL)setup{
databasePath = [[NSBundle mainBundle] pathForResource:@"words" ofType:@"sqlite"];
BOOL isSuccess = YES;
NSFileManager *filemgr = [NSFileManager defaultManager];
if (![filemgr fileExistsAtPath: databasePath ])
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
return isSuccess;
sqlite3_close(database);
}
else {
isSuccess = NO;
NSLog(@"Failed to open/create database");
}
}
return isSuccess;
}
- (NSMutableArray*) findWord:(int)ID
{
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:
@"select Word, Category, Clue, Length, Solved from X where ID=\"%i\"",ID];
const char *query_stmt = [querySQL UTF8String];
NSMutableArray *resultArray = [[NSMutableArray alloc]init];
if (sqlite3_prepare_v2(database,
query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *solved = [[NSString alloc]initWithUTF8String:
(const char *) sqlite3_column_text(statement, 4)];
if ([solved intValue] == 0) {
[resultArray addObject:[NSString stringWithFormat:@"%i", ID]];
NSString *word = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 0)];
[resultArray addObject:word];
NSString *cat = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 1)];
[resultArray addObject:cat];
NSString *clue = [[NSString alloc]initWithUTF8String:
(const char *) sqlite3_column_text(statement, 2)];
[resultArray addObject:clue];
NSString *length = [[NSString alloc]initWithUTF8String:
(const char *) sqlite3_column_text(statement, 3)];
[resultArray addObject:length];
}
else {
}
sqlite3_reset(statement);
sqlite3_close(database);
return resultArray;
}
else{
return nil;
}
}
sqlite3_reset(statement);
}
sqlite3_close(database);
return nil;
}
- (void) findData{
NSMutableArray *m = [[NSMutableArray alloc] init];
m = [self findWord:arc4random() %10];
if (m == nil) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:
@"Data not found" message:nil delegate:nil cancelButtonTitle:
@"OK" otherButtonTitles:nil];
[alert show];
}
else {
NSLog(@"%@", [m objectAtIndex:1]);
}
}
-(IBAction)updateClick:(id)sender{
sqlite3_stmt *updateStmt;
const char *dbpath = [databasePath UTF8String];
if(sqlite3_open(dbpath, &database) == SQLITE_OK)
{
const char *sql = "update X Set Solved = 1 Where ID=2";
if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL)==SQLITE_OK){
}
}
char* errmsg;
sqlite3_exec(database, "COMMIT", NULL, NULL, &errmsg);
if(SQLITE_DONE != sqlite3_step(updateStmt)){
NSLog(@"Error while updating. %s", sqlite3_errmsg(database));
}
sqlite3_finalize(updateStmt);
sqlite3_close(database);
}
任何建议都表示赞赏。
答案 0 :(得分:0)
您正在从捆绑包本身访问数据库。您无法更新任何捆绑资源,因为它们已由证书签名。如果要编辑数据库,请先将其复制到Documents目录中,然后提供文档目录位置的路径,而不是包位置。这将解决您的问题。