sqliteDb を接続する簡単なコードがあります。私の sqlite3_prepare_v2 は繰り返し失敗します。次のコードに絞り込みました。
NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:@"xyz" ofType:@"sqlite3"];
sqliteDb は null を返します。理由はわかりません-できる限りのことを試し、多くのスレッドをたどりました。ここで本当に苦労しています - 助けてください。
sqliteDb を接続する簡単なコードがあります。私の sqlite3_prepare_v2 は繰り返し失敗します。次のコードに絞り込みました。
NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:@"xyz" ofType:@"sqlite3"];
sqliteDb は null を返します。理由はわかりません-できる限りのことを試し、多くのスレッドをたどりました。ここで本当に苦労しています - 助けてください。
xyz.sqlite3
ファイルが正しいターゲット メンバーシップに含まれていますか? 含まれていない場合、ビルド時にバンドルにコピーされません。
これを試して
in.h ファイル
NSString *databasePath;
in .m file
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:@"dbname.sqlite"];
[self checkAndCreateDatabase];
-(void) checkAndCreateDatabase
{
// 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] resourcePath] stringByAppendingPathComponent:databaseName];
// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}