Corroborate if a .sqlite file exists in a folder



  • Hello.

    How to know that a ".sqlite" file exits on the folder? For example if "intersection.sqlite" do not exist in the folder I have to do something and if it do not exist do another thing.

    What I had do is:

    
          QSqlDatabase *ultima;
          ultima=new QSqlDatabase();
          ultima->addDatabase("QSQLITE", "twelve_connection");
          ultima->setDatabaseName(ui->lineEdit_interseccion->text().remove(" ")+".sqlite"); //this is the name of sqlite's file
    
            if(ultima->isOpen())
            {
    
            crearTablaFallasNuevas();
            }
            else
            {
            agregarTablaFallasNuevas();
            }
            delete ultima ;
    

    But for what I see QSqlDatabase::isOpen just compare if ultima run, because it always goes to crearTablaFallasNuevas()


  • Qt Champions 2016

    @Julian
    Hi
    Its best to use QFileInfo as as far as I remember, the SQL open will create new db if not there.

    bool fileExists(QString path) {
        QFileInfo check_file(path);
        // check if file exists and if yes: Is it really a file and no directory?
        if (check_file.exists() && check_file.isFile()) {
            return true;
        } else {
            return false;
        }
    }
    

Log in to reply
 

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