QT+sqlite3 不同数据库之间的表复制

QT+sqlite3 不同数据库之间的表复制


  1. 打开目标数据库
  2. 链接源数据库
  3. 删除可能存在的表
  4. 复制表
  5. 关闭数据库
    {
        QSqlDatabase db_sqlite = QSqlDatabase::addDatabase("QSQLITE",file_target);
        db_sqlite.setDatabaseName(file_target);
        if (!db_sqlite.open()) {
             qDebug()<<"open database error!";
        }else{
            QSqlQuery query(db_sqlite);
            query.exec("drop table if exists test0;");
            QString attach = "attach database '" + file_source + "' as db_source;";
            query.exec(attach);
            query.exec("create table test0 as select * from db_source.test0;");
        }
    }
    QSqlDatabase::removeDatabase(path_project);

!!!注意file_source需要用引号引起来,不然没法识别

https://blog.csdn.net/mad2man/article/details/38144235

你可能感兴趣的:(qt_sqlite)