OGeek|极客世界-中国程序员成长平台

标题: ios - SQLite 数据库不可写 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:35
标题: ios - SQLite 数据库不可写

我有一个使用 Firefox SQLite Manager 插件创建的预填充 SQLite 数据库。 我已将数据库包含到我的项目中,添加到目标并复制到目标组的文件夹中。然后我创建了这个函数来复制文档文件夹中的数据库:

-(void) createEditableDatabase{
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSString *writableDB = [[NSHomeDirectory() stringByAppendingPathComponent"Documents"] stringByAppendingPathComponent"DB.sqlite"];
    success = [fileManager fileExistsAtPath:writableDB];
    if (success){
        return;
    }
    NSString *defaultPath = [[[NSBundle mainBundle] resourcePath]  stringByAppendingPathComponent"DB.sqlite"];
    error = nil;
    success = [fileManager copyItemAtPath:defaultPath toPath:writableDB error:&error];
    if (!success){
        NSAssert1(0, @"Failed to create writable database file:%@", [error localizedDescription]);
    }
    if (error){
        NSLog(@"error = %@", [error localizedDescription]);
    }
}

然后我在 -(void)viewDidLoad 中调用这个函数,如果我 checkin 模拟器文件夹,一旦我启动应用程序就会出现一个 DB 副本。 该应用程序运行良好,我可以使用从数据库中检索到的数据填充 UICollectionView。 然后,当我尝试插入一些数据时,我没有收到错误,但没有数据被添加到数据库中。 这是我在 MyViewController.h 中使用的代码:

@property (nonatomic) NSString *DBPath;
@property (nonatomic) sqlite3 *myAppSQLITE;

这是我在 MyViewController.m 中使用的代码:

-(IBAction)doneid)sender{
    DBPath = [[NSHomeDirectory() stringByAppendingPathComponent"Documents"] stringByAppendingPathComponent"DB.sqlite"];
    const char *dbpath = [DBPath UTF8String];
    sqlite3_stmt *statement;
    if (sqlite3_open(dbpath, &myAppSQLITE) == SQLITE_OK){
        NSString *querySQL = [NSString stringWithFormat"INSERT INTO myTable (name, age) VALUES('frank',30);"];
        const char *query_stmt = [querySQL UTF8String];
        if (sqlite3_prepare_v2(myAppSQLITE, query_stmt, -1, &statement, NULL) == SQLITE_OK){
            sqlite3_finalize(statement);
        }
        sqlite3_close(myAppSQLITE);
    }
}

我没有收到错误/警告,但数据从未更新,即使使用 [self.collectionView reloadData];。如果我在 Firefox 中使用 SQLite Manager 运行相同的查询,一切正常。如果我使用 SQLite 管理器打开 Simulator Documents 文件夹中的数据库,则数据库完好无损,没有更新数据。我在 iPad 上运行应用程序的结果相同。 我该如何解决这个问题? 提前谢谢你。

P.S:大卫的建议是对的,这是创建可编辑数据库的更好方法:

- (void) createEditableDatabase{
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent"DB.sqlite"];
    success = [fileManager fileExistsAtPath:writableDBPath];
    if (success){
        return;
    }
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent"DB.sqlite"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    if (!success) {
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
}

P.S2:这可能是一个适当的错误检查,让我知道它是否正确。

int rc;
while ((rc = sqlite3_step(statement)) == SQLITE_ROW){
    NSLog(@"ROW");
}
if (rc != SQLITE_DONE){
    NSLog(@"%s: step error: %d: %s", __FUNCTION__, rc, sqlite3_errmsg(myAppSQLite));
}



Best Answer-推荐答案


您永远不会通过调用 sqlite3_step 实际执行查询。

你想要:

if (sqlite3_prepare_v2(myAppSQLITE, query_stmt, -1, &statement, NULL) == SQLITE_OK){
    sqlite3_step(statement); // add appropriate error checking
    sqlite3_finalize(statement);
}

关于ios - SQLite 数据库不可写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23369131/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4