-->

Is there a sqlite .dump equivalent in objective-c?

2019-02-06 14:44发布

问题:

I'm trying working on an iOS app that would allow users to sync their sqlite databases via Bluetooth using GameKit. Is there a way to perform the equivalent of .dump on the sqlite shell using any of the many sqlite objective-c libraries?

回答1:

You can create a backup database file, send that over and then do the merging on the destination device. The code to create the backup file is as follows:

- (void) exportDB {

   sqlite3 *sourceDB, *destinationDB;
   sqlite3_backup *sql3Backup;

   NSString *sourceDBPath = @"/path/to/source/database";
   NSString *destinationDBPath = @"/path/to/destination/database";

   if(sqlite3_open([sourceDBPath UTF8String],&sourceDB) != SQLITE_OK){
       NSLog(@"%s\n",sqlite3_errmsg(sourceDB));
       return ;
   }

   if(sqlite3_open([destinationDBPath UTF8String],&destinationDB) != SQLITE_OK){
       sqlite3_close(sourceDB);
       NSLog(@"%s\n",sqlite3_errmsg(destinationDB));
       return;
   }

   sql3Backup = sqlite3_backup_init(destinationDB,"main",sourceDB,"main");
   if(sql3Backup == NULL){
       sqlite3_close(sourceDB);
       sqlite3_close(destinationDB);
       NSLog(@"%s\n",sqlite3_errmsg(destinationDB));
       return;
   }

   if(sqlite3_backup_step(sql3Backup, -1) != SQLITE_DONE){
       NSLog(@"%s\n",sqlite3_errmsg(destinationDB));
       return;
   }

   if(sqlite3_backup_finish(sql3Backup) != SQLITE_OK){
       NSLog(@"%s\n",sqlite3_errmsg(destinationDB));
       return;
   }

   sqlite3_close(sourceDB);
   sqlite3_close(destinationDB);
}


回答2:

I don't think so. But, you could exec a SELECT * statement and then iterate over argc in the callback function.

Something like

void callback (void *param, int argc, char **argv, char **azColName)
{
    for(int i=0;i<argc;i++)
    {
        get ... azColName[i] ... argv[i]
    }
}