0

iOSでSQLiteデータベースを作成してアクセスしようとするのはこれが初めてで、チュートリアルに従いましたが、これを機能させることはできません. これらは私が従った手順です:

  1. Firefox プラグイン (「testDatabase」) と複数の列を持つテーブル (「testTable」) を使用してデータベースを作成しました。
  2. 「testDatabase.sqlite」ファイルをディレクトリに保存しました
  3. そのようなファイルを Xcode プロジェクトの「Supporting Files」グループにドラッグ アンド ドロップしました
  4. Build Phases > Link Binary With Libraries に "libsqlite3.dylib" を追加
  5. Build Settings > Linking > Other Linker Flags > Debug and Release に「-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"];.

前もって感謝します

4

1 に答える 1

2

/ Users / [user] / Library / Application Support / iPhone Simulator / 5.1 / Applications /で生成されたアプリフォルダーを削除するだけで 、シミュレーターの実行時に再度作成されるようになりました。

于 2012-10-15T06:59:12.410 に答える