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?
Asked
Active
Viewed 1,856 times
11
-
1There is no ready-to-use class for this dump-command so far. It shouldn't be too difficult reading all tables and generating a dump.sql with the INSERT staements. – ott-- Sep 16 '11 at 00:13
-
1Something that might help: You can get the catalog of tables by making a query to the system table 'sqlite_master' (i.e., "SELECT * FROM sqlite_master;"). If you iterate over the results, you can get the table name from the 'tbl_name' field and the 'sql' field contains the 'CREATE TABLE' statement. Parse the latter to get the field names. – mpemburn Oct 13 '11 at 13:45
2 Answers
3
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);
}

Omniwombat
- 744
- 7
- 16
-1
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]
}
}

Max
- 104
- 3
-
Can you expand on this answer to give a bit more context? I think I understand more or less what you're saying, but I'm not quite sure how it would work in an actual app. – buildsucceeded Jun 25 '13 at 17:17