Hello,
I don't think it's that simple as you try to make it, at least if you want to include the data too.
Few years ago I had a similar problem and I wrote a function for that purpose. I'm copying-pasting the code from my old post after some reformatting to be readable. Although I have improved the code a bit since then in my own app, this should help you get started (please check the indentation, some fixes may be needed). This function doesn't transfer the schema, but I think it's easy to include it.
void DbManager::exportTables()
{
QHash<QString,QStringList> tablesWithFields; //It holds the table name and its fields
QStringList tables = sourceDb.tables();
QSqlQuery query(sourceDb);
foreach(const QString &table,tables) {
query.exec(QString("PRAGMA TABLE_INFO(%1)").arg(table));
QStringList fields;
while(query.next()) {
fields << query.value(1).toString();
}
tablesWithFields.insert(table,fields);
}
QFile f(QDir::homePath() + "/myDump.sql");
f.open(QIODevice::Append | QIODevice::Text);
QTextStream streamer(&f);
//If constraints can't be dropped in the target database, some reordering of
//the INSERT statements may be needed
QStringList sortedTables = tablesWithFields.keys();
sortedTables.move(sorted.indexOf("table1"),0);
...
streamer << "BEGIN;\n";
foreach(const QString &table,sortedTables) {
if(table=="sqlite_sequence" /*|| table=="table4", etc*/) continue;
QString statement = QString("INSERT INTO %1 VALUES('").arg(table);
QStringList fields = tablesWithFields.value(table);
QString fieldsString = fields.join(",");
query.exec(QString("SELECT %1 FROM %2").arg(fieldsString).arg(table));
if(!query.next()) continue;
query.previous();
while(query.next()) {
for(int i=0; i < fields.size(); ++i) {
QString value = query.value(i).toString();
value.replace("'","''"); //Handle single quotes inside strings
if(value.isEmpty()) {
value = "NULL";
statement.chop(1); //NULL should not appear inside quotes
statement.append(value+",'");
} else {
statement.append(value+"','");
}
}
statement.chop(2); //Remove comma and single quote from the end of value group
statement.append("),('"); //Close the value group and start a new one
}
statement.chop(3);//Remove comma, opening parenthesis, single quote from the end
streamer << statement << ";\n"; //Complete the INSERT statement
}
streamer << "COMMIT;";
f.close();
}
And then batch execute the sql file like this:
...
exportTables();
QSqlQuery query(targetDb);
QFile f(QDir::homePath()+"/myDump.sql");
f.open(QIODevice::ReadOnly | QIODevice::Text);
if(!query.exec(f.readAll()))
qCritical() << "Can't execute sql file: " << query.lastError().text();