-1

sqliteデータベースのテーブルでSelectを作成できません。

.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);

ログには「get」のみが表示されます。「テスト」はありません。

誰かが私を助けてくれることを願っています。

4

3 に答える 3

0

指定されたデータベースが存在しない場合、sqlite3_open は新しい (空の) データベースを作成します。これが起こっていないことを確認するには、代わりに sqlite3_open_v2 を使用してみて、フラグ引数として SQLITE_OPEN_READWRITE を使用してください。これにより、存在しないデータベースを開こうとするとエラーが発生します。

于 2012-07-14T09:57:15.830 に答える
0

のパラメータに渡しnilているため、データベースのコピー コードが機能していないと思います。正しいファイル拡張子を渡してみて、コピー操作の成功または失敗を検出するコードを追加し (そしてメソッドを returnにします)、そこから取得します。ofType[NSBundle pathForResource]checkAndCreateDBBOOL

あなたのデータベース選択コードは私には問題ないように見えるので、@Michealの回答で説明されているように、空のデータベースがあると思います。

于 2012-07-14T10:07:41.800 に答える
-1

ここで SQLite を使用するためのサンプル プロジェクトを参照できます: https://github.com/AaronBratcher/ABSQLite

より伝統的なデータベースの方法で SQLite にアクセスするためのクラスがあり、それが作業を容易にします。

于 2013-07-11T15:22:26.280 に答える