Database Import && Export



  • Hello all.
    In My Project I Have Database Export And Import.

    What is it Database Export?
    Export:

    void MainWindow::on_actionExport_triggered()
    {
        QDir *dr = new QDir();
    
        if(QFile::copy(dr->absolutePath() + "\\database.sqlite", QFileDialog::getSaveFileName(0,"Export Database","database.sqlite"))){
            QMessageBox::information(this,"Money Exchange","ბაზის ექსპორტი დასრულდა წარმატებით");
        }else{
            QMessageBox::critical(this,"Money Exchange","ბაზის ექსპორტი ვერ მოხერხდა");
        }
    }
    

    What Is Import?
    Import:
    I want to choose a file and copy it

    void MainWindow::on_actionImport_triggered()
    {
        QDir *dr = new QDir();
    
        if(QFile.copy(QFileDialog::getOpenFileName(0,"Import Database","database.sqlite"),dr->absolutePath() + "\\database.sqlite")){
            QMessageBox::information(this,"Money Exchange","ბაზის იმპორტი დასრულდა წარმატებით");
        }else{
            QMessageBox::critical(this,"Money Exchange","ბაზის იმპორტი ვერ მოხერხდა" + file.errorString());
        }
    }
    

    Export works.
    But the import is not fulfilled.
    I think, i know the reason... I'm importing the base in the same folder where the file is named "database.sqlite" and it's already in the process..

    What can we do?
    How to disable database.sqlite? (If it really makes a error).



  • According Qt documentation for QFile::copy() :
    "If a file with the name newName already exists, copy() returns false (i.e., QFile will not overwrite it)."
    I think this is your problem. To solve it you can first rename or remove original file "database.sqlite" and then copy the new one.
    Also, probably you have to close all open connections to database if they exists.



  • @Stoyan
    SOLVED! Thanks



  • @Stoyan said in Database Import && Export:

    Also, probably you have to close all open connections to database if they exists.

    Also, how can this be done?


  • Moderators

    Hi,

    Here is link to the docs



  • @Eddy
    Hi,
    So do I remove and call again the base?


  • Moderators

    Yes that's the general idea.

    Eddy



  • @Eddy
    I Tried this:

        tempdb = QSqlDatabase::addDatabase("QSQLITE","taz");
        tempdb.setDatabaseName("C:\\Users\\tleladze\\Desktop\\dbbb.sqlite");
    
        if(globalall->tempdb.open()){
            qDebug() << "TOP OPEN";
        }
    
        QSqlDatabase::removeDatabase("taz");
    
        if(globalall->tempdb.open()){
            qDebug() << "BOTTOM OPEN";
        }else{
            qDebug() << "NOT OPEN";
        }
    
        QFile cp;
        if(cp.copy("C:\\Users\\tleladze\\Desktop\\MoneyExchange\\dbbb.sqlite","C:\\Users\\tleladze\\Desktop\\dbbb.sqlite")){
            qDebug() << "YES";
        }else{
            qDebug() << cp.errorString();
        }
    

    Connection is close, but what do I do wrong now?



  • Debug answers:

    TOP OPEN
    QSqlDatabasePrivate::removeDatabase: connection 'taz' is still in use, all queries will cease to work.
    NOT OPEN
    "Unknown error"
    


  • @Taz742
    Before to use

    QSqlDatabase::removeDatabase("taz");
    

    you have to close database:

    tempdb.close();
    

    This way you close all queries and release used resources.
    Also you have to open database again after the copy part. That's the reason you need to close it.
    And you didn't rename or remove old file "C:\Users\tleladze\Desktop\dbbb.sqlite" before copying the new one.
    I am not sure whether it is necessary to use removeDatabase() or it will be enough to close it.
    The idea is the file of the database ("dbbb.sqlite") to NOT be used by your program, so it can be deleted or renamed.



  • @Stoyan
    Thank You Very Much.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.