Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Database Connection Error

Database Connection Error

Scheduled Pinned Locked Moved Solved General and Desktop
databasesqlitesqlite database
5 Posts 3 Posters 3.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M4RZB4NiM Offline
    M4RZB4NiM Offline
    M4RZB4Ni
    wrote on last edited by M4RZB4Ni
    #1

    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();
    }
    

    Thanks
    M4RZB4Ni

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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 :)

        M4RZB4NiM 1 Reply Last reply
        1
        • mrjjM mrjj

          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 :)

          M4RZB4NiM Offline
          M4RZB4NiM Offline
          M4RZB4Ni
          wrote on last edited by
          #4

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

          Thanks
          M4RZB4Ni

          mrjjM 1 Reply Last reply
          1
          • M4RZB4NiM M4RZB4Ni

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

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved