问题
In my app I am doing read ,write and delete on sqlite database(in db form) and it work fine in simulator but not working in actual device (giving error: No such table found
).I try very hard to look for solution but couldn't find one or may be i misread something.
My db is in the
/users/My Name/library/developer/Core simulator/devices/devicen-id/data/application/app-id/documents/My database.
- (NSString *) getWritableDBPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:mydaatabase];
}
I am confuse where did i need to copy my database while running in the actual iOS device and what is the core problem.Any help regarding this will be very helpful
-(void)createEditableCopyOfDatabaseIfNeeded
{
BOOL success;
NSFileManager *fileManager1 = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:mydaatabase];
NSLog(@"=======%@", writableDBPath);
success = [fileManager1 fileExistsAtPath:writableDBPath];
if (success)
return;
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:mydaatabase];
NSLog(@"=======%@", [NSBundle mainBundle]);
success = [fileManager1 copyItemAtPath:defaultDBPath
toPath:writableDBPath
error:&error];
if(!success)
{
NSAssert1(0,@"Failed to create writable database file with Message : '%@'.",
[error localizedDescription]);
}
}
回答1:
Check your .sqlite
file is added to your Copy Bundle Resources
in Build Phases
of your project. If it is not there, add it in your project.
Moreover, I can't find code for opening the database. So, in your ViewController.h
file, declare Sqlite *database;
In your ViewController.m
file,
-(void)databaseOpen
{
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory=[paths objectAtIndex:0];
NSString *myDBnew=[documentsDirectory stringByAppendingPathComponent:@"yourSqliteFile.sqlite"];
database =[[Sqlite alloc]init];
[database open:myDBnew];
NSLog(@"path: %@", myDBnew);
NSLog(@"Database opened");
}
and call it wherever you want as [self databaseOpen];
and then write your query.
Also, your -(void)createEditableCopyOfDatabaseIfNeeded
should look something like this in your AppDelegate.m
:
- (void)createEditableCopyOfDatabaseIfNeeded
{
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"yourSqliteFile.sqlite"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success) return;
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"yourSqliteFile.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
Call -(void)createEditableCopyOfDatabaseIfNeeded
in didFinishLaunchingWithOptions:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[self createEditableCopyOfDatabaseIfNeeded];
return YES;
}
This should get you going.
回答2:
Here are steps to working with SQLite Database.
Create your database in SQLite, add required table with well structure. and add it to your application.
Now, go to your app, select project from left panel, in middle panel -> General -> Linked Frameworks and Libraries(Last one) add
libsqlite3.0.tbd
(it is depend upon your Xcode version.)Now, define below methods in
your-object.h
#import <sqlite3.h> + (NSString *)getDBPath; + (void)copyDatabaseIfNeeded; + (void)openDatase;
Implement these in
your-object.m
static sqlite3 *database; #pragma mark - Copy and open databse + (NSString *)getDBPath { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES); NSString *documentsDir = [paths objectAtIndex:0]; return [documentsDir stirngByAppendingPathComponent:@"dbName.sqlite"]; } + (void)copyDatabaseIfNeeded { NSFileManager *fileManager = [NSFileManager defaultManager]; NSError *error; NSString *dbPath = [self getDBPath]; BOOL success = [fileManager fileExistsAtPath:dbPath]; if(!success) { NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"dbName.sqlite"]; success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error]; if (!success) NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); } } + (void)openDatase { NSString *dbPath = [self getDBPath]; if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) { NSLog(@"Database Successfully Opened."); } else { NSLog(@"Error in opening database."); } }
Then call method in
AppDelegate.m
inapplication: didFinishLaunchingWithOptions:
[DataManager copyDatabaseIfNeeded];
Whenever you want to perform task with database, open it and check.
For inserting in database
+ (void)insertData
{
const char *dbpath = [[self getDBPath] UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *strSQL = [NSString stringWithFormat:@"INSERT INTO table name………………."];
const char *insertQuery = [strSQL UTF8String];
sqlite3_stmt *insert_stmt;
if (sqlite3_prepare_v2(database, insertQuery, -1, &insert_stmt, NULL) == SQLITE_OK)
{
if (sqlite3_step(insert_stmt) == SQLITE_DONE)
{
// Code
}
else
{
NSLog(@"error");
}
}
sqlite3_finalize(insert_stmt);
sqlite3_close(database);
}
}
7. Completed.... enjoy with database.
来源:https://stackoverflow.com/questions/33294747/my-sqlite-database-in-db-form-not-working-on-actual-device-but-work-fine-in-simu