iOSでSQLiteデータベースを作成してアクセスしようとするのはこれが初めてで、チュートリアルに従いましたが、これを機能させることはできません. これらは私が従った手順です:
- Firefox プラグイン (「testDatabase」) と複数の列を持つテーブル (「testTable」) を使用してデータベースを作成しました。
- 「testDatabase.sqlite」ファイルをディレクトリに保存しました
- そのようなファイルを Xcode プロジェクトの「Supporting Files」グループにドラッグ アンド ドロップしました
- Build Phases > Link Binary With Libraries に "libsqlite3.dylib" を追加
- Build Settings > Linking > Other Linker Flags > Debug and Release に「-lsqlite3」を追加
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; }
次に、データベースに書き込もうとしています:
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"];
.
前もって感謝します