我的db形式的sqlite数据库无法在实际设备上运行,但在模拟器中可以正常运行 [英] My sqlite database in db form not working on actual device but work fine in simulator

查看:100
本文介绍了我的db形式的sqlite数据库无法在实际设备上运行,但在模拟器中可以正常运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我正在对sqlite数据库(以db形式)进行读取,写入和删除,它在模拟器中可以正常工作,但在实际设备中则无法工作(给出错误:未找到此类表)。我非常努力地寻找解决方案,但是找不到一个解决方案,或者可能是我误读了一些内容。

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.

我的数据库在


/ users / My Name / library / developer / Core
Simulator / devices / devicen-id / data / application / app-id / documents /我的
数据库。

/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]);
}
}


推荐答案

检查您的 .sqlite 文件在构建阶段副本捆绑资源 $ c>您的项目。如果不存在,请将其添加到您的项目中。

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.

此外,我找不到用于打开数据库的代码。因此,在您的 ViewController.h 文件中,声明 Sqlite * database;

Moreover, I can't find code for opening the database. So, in your ViewController.h file, declare 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]; ,然后编写您的查询。

and call it wherever you want as [self databaseOpen]; and then write your query.

此外,您的-(void)createEditableCopyOfDatabaseIfNeeded 在您的 AppDelegate.m 中应该看起来像这样:

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]);
    }


}

通话<$ didFinishLaunchingWithOptions中的c $ c>-(void)createEditableCopyOfDatabaseIfNeeded

Call -(void)createEditableCopyOfDatabaseIfNeeded in didFinishLaunchingWithOptions:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    [self createEditableCopyOfDatabaseIfNeeded];
    return YES;
}

这应该可以帮助您。

这篇关于我的db形式的sqlite数据库无法在实际设备上运行,但在模拟器中可以正常运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆