我的SQL数据库在数据库forms不工作的实际设备,但在模拟器中工作正常
在我的应用程序中,我正在读取,写入和删除sqlite数据库(以dbforms),它在模拟器中工作正常,但在实际设备中不工作(给出错误: No such table found
)。我努力寻找解决scheme,但找不到一个或可能是我误解了一些东西。
我的数据库是在
/ 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]; }
我很疑惑我在哪里需要复制我的数据库,而在实际的iOS设备上运行,什么是核心问题。任何帮助,这将是非常有帮助的
-(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]); } }
检查您的.sqlite
文件是否添加到您的项目的Build Phases
的Copy Bundle Resources
。 如果不存在,请将其添加到您的项目中。
而且,我找不到打开数据库的代码。 所以,在你的ViewController.h
文件中,声明Sqlite *database;
在您的ViewController.m
文件中,
-(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"); }
并把它称为任何你想要的[self databaseOpen];
然后编写你的查询。
另外,你的-(void)createEditableCopyOfDatabaseIfNeeded
在你的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]); } }
在didFinishLaunchingWithOptions:
调用-(void)createEditableCopyOfDatabaseIfNeeded
didFinishLaunchingWithOptions:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. [self createEditableCopyOfDatabaseIfNeeded]; return YES; }
这应该让你去。
以下是使用SQLite数据库的步骤。
-
在SQLite中创build你的数据库,添加所需的表结构。 并将其添加到您的应用程序。
-
现在,进入您的应用程序,从左侧面板中select项目,在中间面板 – >常规 – >链接框架和库(最后一个)添加
libsqlite3.0.tbd
(这取决于您的Xcode版本)。 -
现在,在
your-object.h
定义下面的方法#import <sqlite3.h> + (NSString *)getDBPath; + (void)copyDatabaseIfNeeded; + (void)openDatase;
-
在
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."); } }
-
然后在
application: didFinishLaunchingWithOptions:
中的AppDelegate.m
中调用方法application: didFinishLaunchingWithOptions:
[DataManager copyDatabaseIfNeeded];
-
每当你想用数据库执行任务时,打开它并检查。
用于插入数据库
+ (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.完成….享受与数据库。