How to update SQLite DB on iPhone app update?

前端 未结 3 1321
萌比男神i
萌比男神i 2020-12-24 09:15

I currently have an iPhone app in the iTunes app store that uses a SQLite database as a datastore. The user can sync the app against a web service and data that is returned

相关标签:
3条回答
  • 2020-12-24 09:44

    We check for updates by querying the server for a hash value in the header of a plist that is spit out via a php file that queries the server database. You can store that hash locally and compare it to the last time the app ran. That way the phone knows if its version is out of date. Then we download the new plist in the background and update the database on the phone.

    EDIT:

    At the end of our php, we get an MD5 of the XML output of the plist we are generating on the server like this:

    header("MD5-Hash: ". md5($xml_output));
    echo $xml_output;
    

    Then we get the hash of the plist on the iPhone from userDefaults like so:

    NSUserDefaults* defaults  = [NSUserDefaults standardUserDefaults];
    curHash = [defaults stringForKey:kUpdateUserDefault];
    

    And the server's hash from the NSURLRequest, like so:

    NSString *hash = [[[res allHeaderFields] objectForKey:kUpdateHeaderField] retain];
    

    Then we compare the two and start the download only if the hashes don't match:

        if (![curHash isEqualToString:hash]) {
                [self performSelector:@selector(sendUpdateStarted) onThread:[NSThread mainThread] withObject:nil waitUntilDone:NO];
                ... download the file and save it as the new iPhone's plist
        }
    

    This code is written by my very capable partner, Oliver Rice.

    0 讨论(0)
  • 2020-12-24 09:50

    sqlite3 *database; sqlite3_stmt *update_statement = nil;

    if(sqlite3_open([strDatabasePath UTF8String], &database) == SQLITE_OK)
    {
       nsstring  *strMQueryupdate="write your query here";
    
        const char *sql = [strMQueryupdate UTF8String];
    
        if (sqlite3_prepare_v2(database, sql, -1, &update_statement, NULL) != SQLITE_OK) {
            NSLog(@"update fails");
        }
        else
        {
            sqlite3_bind_text(update_statement, 1, [[arrayname objectAtIndex:0]  UTF8String], -1, SQLITE_TRANSIENT);
    
    
            int success = sqlite3_step(update_statement);
            sqlite3_reset(update_statement);
            if (success == SQLITE_ERROR){}
            else {}
        }
        sqlite3_finalize(update_statement);
    }
    sqlite3_close(database);   
    
    0 讨论(0)
  • 2020-12-24 09:59

    You should store the current version number of the app somewhere persistent -- in the database or better yet in the Defaults database. On startup, your app will compare it's own version number with the persisted version number. If they differ, then this is the first run after an update. If the persisted version number isn't there, then obviously also this is the first run after an update.

    As for updating your database, if it's in place you would use the usual SQL ALTER commands to update your schema, and do any database data migration at the same time.

    0 讨论(0)
提交回复
热议问题