Download and use sqlite file in iOS

2019-04-02 23:33发布

I have successfully used a .sqlite file when I store it in my project folder. I'm now trying to do the same thing, except pull the file from online instead of storing it locally. Any suggestions based on this code? I'm getting the error message "Problem with prepare statement" from the bottom of the code.

    NSData *fetchedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://www.dropbox.com/s/zpcieluo2qv43vy/builds.sqlite?dl=1"]];
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"builds.sqlite"];
    [fetchedData writeToFile:filePath atomically:YES];

    NSFileManager *fileMgr = [NSFileManager defaultManager];

    BOOL success = [fileMgr fileExistsAtPath:filePath];
    if (!success) {
        NSLog(@"Cannot locate database file '%@'.", filePath);
    }
    if (!(sqlite3_open([filePath UTF8String], &db) == SQLITE_OK)) {
        NSLog(@"An error has occured.");
    }
    const char *sql = "SELECT * FROM builds";
    sqlite3_stmt *sqlStatement;

    if (sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK) {
        NSLog(@"Problem with prepare statement");
    }

1条回答
【Aperson】
2楼-- · 2019-04-02 23:52

Your dropbox URL will not work, you need to use: https://www.dropbox.com/s/zpcieluo2qv43vy/builds.sqlite?dl=1

or you are just downloading a webpage...

查看更多
登录 后发表回答