Objective c 目标c sqlite无选择工程

Objective c 目标c sqlite无选择工程,objective-c,Objective C,我无法对sqlite数据库中的表进行选择 我有以下代码将.sqlite文件复制到用户目录: // copy the database to the user's directory - (void)checkAndCreateDB { // Check if the SQL database has already been saved to the users phone, if not then copy it over BOOL success; // C

我无法对sqlite数据库中的表进行选择

我有以下代码将.sqlite文件复制到用户目录:

    // copy the database to the user's directory
- (void)checkAndCreateDB {
    // Check if the SQL database has already been saved to the users phone, if not then copy it over
    BOOL success;

    // Create a FileManager object, we will use this to check the status
    // of the database and to copy it over if required
    NSFileManager *fileManager = [NSFileManager defaultManager];

    // Check if the database has already been created in the users filesystem
    success = [fileManager fileExistsAtPath:databasePath];

    // If the database already exists then return without doing anything
    if(success) return;

    // If not then proceed to copy the database from the application to the users filesystem

    // Get the path to the database in the application package
    NSString *databasePathFromApp = [[NSBundle mainBundle] pathForResource:databaseName ofType:nil];
    // Copy the database from the package to the users filesystem
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
    [fileManager release];
}
并在此处选择:

sqlite3 *database;

categories = [[NSMutableArray alloc] init];

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
        const char *sqlStatement = "select * from category";
        sqlite3_stmt *compiledStatement;
    NSLog(@"get");
        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
        NSLog(@"test");
        while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
                NSString *cId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
                NSString *cName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];

                Category *category = [[Category alloc] initWithId:cId name:cName];
                [categories addObject:category];
                [categories release];
            } 

    }
        // Release the compiled statement from memory
        sqlite3_finalize(compiledStatement);

    }
    sqlite3_close(database);
日志仅显示:“获取”。“测试”不存在


我希望有人能帮助我。

如果指定的数据库不存在,sqlite3\u open将创建一个新的(空)数据库。为了确保不会发生这种情况,请尝试改用sqlite3\u open\u v2,并使用SQLITE\u open\u READWRITE作为flags参数。这样,如果您试图打开一个不存在的数据库,就会出现错误。

我相信您的数据库副本代码不起作用,因为您正在将
nil
传递给
[NSBundle pathForResource]
of type
参数。尝试传递正确的文件扩展名并添加代码以检测复制操作的成功或失败(并使您的
checkAndCreateDB
方法返回
BOOL
),然后从中获取


您的数据库选择代码在我看来还行,所以我猜您的数据库是空的,如@Micheal的回答所述。

这里有一个使用SQLite的示例项目,您可以参考:


它有一些类,用于以一种更传统的数据库方式访问SQLite,我觉得这会让事情变得更简单。

Hi Michael。谢谢你的回答。它仍然不起作用。if(sqlite3_open_v2([databasePath UTF8String],&database,SQLITE_open_READWRITE,NULL)=SQLITE_OK){是我的新代码。所以你认为复制代码不起作用吗?在所有if语句中添加类似NSLog(@“Error:%s”,sqlite3_errmsg(database))的else块,它将显示错误。这将简化对错误的理解。