Database Connection Error



  • Hi,
    I want to Connect My sqlite database to my program
    i wrote below code,but In any situation program say Connected!
    Even when i changed database name
    my db in my root of program.
    only when i change the db url in my code,program say error!
    what is the problem?
    and what i must do?
    code:

    #include "mainwindow.h"
    #include <QApplication>
    #include "QSqlDatabase"
    #include <QMessageBox>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QSqlDatabase db= QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName("webappsstore.sql");
        if(!db.open())
        {
        QMessageBox msg;
        msg.setText("Faild!");
        msg.exec();
        }else{
          QMessageBox msg;
          msg.setText("Connected!");
          msg.exec();
        }
        MainWindow w;
        w.show();
        return a.exec();
    }
    

  • Lifetime Qt Champion

    Hi,

    Because with SQLite, if you don't give the path to an existing database file, a new one will be created. But that will only happen if you don't try to open in a read-only folder. You can get the error with QSqlDatabase::lastError.


  • Qt Champions 2017

    just a note
    in c++ when you use \ you must use 2 \
    so if u had
    something like
    db.setDatabaseName("c:\myproject\webappsstore.sql");
    it might have worked if u did
    db.setDatabaseName("c:/myproject/webappsstore.sql");
    its just a note. might not been your issue :)



  • @mrjj
    @SGaist
    tnx
    i must removed .sql suffix from my database name in my codes!


  • Qt Champions 2017

    @M4RZB4Ni
    Ok ?
    I normally used .DB but didn't now it did mean anything ??


Log in to reply
 

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