gpt4 book ai didi

ios - 无法访问 iOS SQLite 数据库 : sqlite3_prepare_v2 and 'no such table' sqlite3_errmsg

转载 作者:行者123 更新时间:2023-11-28 22:47:31 26 4
gpt4 key购买 nike

这是我第一次尝试在 iOS 中创建和访问 SQLite 数据库,我遵循了教程,但我无法完成这项工作。这些是我遵循的步骤:

  1. 通过 Firefox 插件(“testDatabase”)和一个包含多列的表(“testTable”)
  2. 将“testDatabase.sqlite”文件保存在一个目录中
  3. 将此类文件拖放到我的“支持文件”组中Xcode项目
  4. 在构建阶段添加“libsqlite3.dylib”> 将二进制文件链接到图书馆
  5. 在build设置 > 链接 > 其他链接器标志中添加了“-lsqlite3” >调试与发布
  6. 在 ViewController 中,调用此方法:

    -(NSString *)copyDatabaseToDocuments {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"testDatabase.sqlite"];
    if ( ![fileManager fileExistsAtPath:filePath] ) {
    NSString *bundlePath = [[[NSBundle mainBundle] resourcePath]
    stringByAppendingPathComponent:@"testDatabase.sqlite"];
    [fileManager copyItemAtPath:bundlePath toPath:filePath error:nil];
    }
    return filePath;
    }
  7. 然后,尝试在数据库中写入:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"testDatabase.sqlite"];
    sqlite3 *database;

    if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) {
    const char *sqlStatement = "insert into testTable (id) VALUES (?)";
    sqlite3_stmt *compiledStatement;

    if (sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {

    sqlite3_bind_int(compiledStatement, 1, newObject.ID);
    }

    if(sqlite3_step(compiledStatement) == SQLITE_DONE) {
    sqlite3_finalize(compiledStatement);
    }
    }
    sqlite3_close(database);

我已经阅读了几篇关于类似错误的帖子,但我尝试过的都没有对我有用......我也尝试过 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"testDatabase"ofType: @"sqlite"]; 但它是空的,而且还多次在模拟器中低估和重新部署我的应用程序......

提前致谢

最佳答案

我最终通过简单地删除在 /Users/[user]/Library/Application Support/iPhone Simulator/5.1/Applications/ 中生成的应用程序文件夹来运行模拟器,以便在运行模拟器时再次创建

关于ios - 无法访问 iOS SQLite 数据库 : sqlite3_prepare_v2 and 'no such table' sqlite3_errmsg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12814458/

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