0

まず、「 subject.sqlite 」を作成し、それを Xcode ファイルの「 Copy Bundle Resrources 」に挿入します。

このコードを使用しました

NSData *fetchedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.precaliga.com/iphone/subjects.sqlite"]];
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"subjects.sqlite"];
    [fetchedData writeToFile:filePath atomically:YES];

    NSFileManager *fileMgr = [NSFileManager defaultManager];

    BOOL success = [fileMgr fileExistsAtPath:filePath];
    if (!success) {
        NSLog(@"Cannot locate database file '%@'.", filePath);
    }
    else {
        self.db = [DBController sharedDatabaseController:filePath];
        DataTable* table = [_db  ExecuteQuery:@"SELECT * FROM subjects"];
        NSLog(@"%@",table.columns);
        NSLog(@"Downloaded Successfuly ..");
    }

しかし、「 table.columns 」が返されます

2013-04-12 11:34:06.202 プレカリガ[82123:c07] ( )

ダウンロードしたsqliteファイルを読み取らないためです。関数をダウンロードした後にデータを読み取るにはどうすればよいですか

前もって感謝します..私を助けてください

4

1 に答える 1

0

これを試して

NSData *fetchedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.precaliga.com/iphone/subjects.sqlite"]];
NSLog(@"%d",fetchedData.length);
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"subjects.sqlite"];
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath])
     [[NSFileManager defaultManager] createFileAtPath:filePath contents:fetchedData attributes:nil];
[fetchedData writeToFile:filePath atomically:YES];

NSFileManager *fileMgr = [NSFileManager defaultManager];

BOOL success = [fileMgr fileExistsAtPath:filePath];
if (!success)
{
    NSLog(@"Cannot locate database file '%@'.", filePath);
}
else
{
    NSLog(@"Downloaded Successfuly ..");
}

編集

これらを AppDelegate.m に記述します

- (void) copyDatabaseIfNeeded
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSString *folderPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
    // First, test for existence.
    if (![fileManager fileExistsAtPath:folderPath])
        [fileManager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:&error]; //Create folder

    NSString *dbPath = [folderPath stringByAppendingPathComponent:@"subjects.sqlite"];
    BOOL success = [fileManager fileExistsAtPath:dbPath];

    if(!success)
    {
        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"subjects.sqlite"];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
        if (!success)
            NSAssert1(0, @"subjects.sqlite : Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    /** Database copy is not exist **/
    [self copyDatabaseIfNeeded]; // Call this way
}
于 2013-04-12T09:39:04.857 に答える