Results 1 to 11 of 11

Thread: cannot open include file qsqldatabase

  1. #1
    Join Date
    Apr 2012
    Location
    Romania
    Posts
    22
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default cannot open include file qsqldatabase

    Hello. I've tried to right an app with Qt 5.1.1 using sql. When I build my app I get the following error: "Cannot open include file: 'QSqlDatabase': No such file or directory". I've already put the line in the .pro file: QT += sql

    Has anyone any ideas what can I do? Thanks in advance.

    P.S.: The part of my application with sql code is quite simple:

    Qt Code:
    1. QSqlDatabase db(QSqlDatabase::addDatabase("QMYSQL"));
    2.  
    3. //connect to database
    4. db.setDatabaseName("sarbatori.mwb");
    5.  
    6. if ( !db.open() )
    7. QMessageBox::critical(0, "Error", db.lastError().text());
    8. else
    9. QMessageBox::information(0, "OK", "Merge!!!");
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: cannot open include file qsqldatabase

    Hello , did you include QtSql in you'r file ?

    just a note, dont you need a connection string or a dsn to open the connection with a database ?
    something like :
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    2. QString servername = "X-PC\\LOCALHOST";
    3. QString dbname = "CARSQL";
    4. QString connTemplate = "DRIVER={SQL SERVER};SERVER=%1;DATABASE=%2;";
    5. QString connectionString = connTemplate.arg(servername).arg(dbname);
    6. db.setDatabaseName(connectionString);
    To copy to clipboard, switch view to plain text mode 

    or

    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
    2. db.setHostName("acidalia");
    3. db.setDatabaseName("customdb");
    4. db.setUserName("mojito");
    5. db.setPassword("J0a1m8");
    To copy to clipboard, switch view to plain text mode 
    Last edited by toufic.dbouk; 22nd September 2013 at 22:51.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: cannot open include file qsqldatabase

    Qt Code:
    1. I've already put the line in the .pro file: QT += sql
    2. Has anyone any ideas what can I do?
    To copy to clipboard, switch view to plain text mode 
    Yes, run qmake after changing the PRO file.

    Your "quite simple" code is also unlikely to work when it compiles. A MySQL Workbench file is not a MySQL database.

  4. #4
    Join Date
    Apr 2012
    Location
    Romania
    Posts
    22
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: cannot open include file qsqldatabase

    Quote Originally Posted by ChrisW67 View Post
    Yes, run qmake after changing the PRO file.
    Thanks. It works.

    Quote Originally Posted by ChrisW67 View Post
    Your "quite simple" code is also unlikely to work when it compiles. A MySQL Workbench file is not a MySQL database.
    Thanks. I'm a little new in working with Qt and MySQL. I'll work with the console to create my database. Is there a way to find out in which file the database created is saved?


    Added after 9 minutes:


    Quote Originally Posted by toufic.dbouk View Post

    just a note, dont you need a connection string or a dsn to open the connection with a database ?
    something like :
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    2. QString servername = "X-PC\\LOCALHOST";
    3. QString dbname = "CARSQL";
    4. QString connTemplate = "DRIVER={SQL SERVER};SERVER=%1;DATABASE=%2;";
    5. QString connectionString = connTemplate.arg(servername).arg(dbname);
    6. db.setDatabaseName(connectionString);
    To copy to clipboard, switch view to plain text mode 

    or

    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
    2. db.setHostName("acidalia");
    3. db.setDatabaseName("customdb");
    4. db.setUserName("mojito");
    5. db.setPassword("J0a1m8");
    To copy to clipboard, switch view to plain text mode 
    Well I'm working on localhost and with no password or specific user name. I thought qt will implicit put localhost, root and no password. I have another question: How do you specify to which file the app should connect?
    Last edited by Saurian; 23rd September 2013 at 07:56.

  5. #5
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: cannot open include file qsqldatabase

    Qt Code:
    1. QString dbname = "CARSQL"; /* thats the name of your created database */
    To copy to clipboard, switch view to plain text mode 

    in case you want to use a dsn, you can set the database name in the options
    for windowspen control panel , go to administrative tools , open Data Sources (ODBC) , add a new system dsn with the specified database and driver (make sure to open the right version 32 or 64-bit
    then you can just use:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    2. db.setDatabaseName("yourDSN");
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by Saurian
    Thanks. It works.
    you should always save and rebuild the project for indexing and parsing if you add new code to the .pro file.

    Good Luck
    Last edited by toufic.dbouk; 23rd September 2013 at 11:15.

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: cannot open include file qsqldatabase

    Quote Originally Posted by Saurian View Post
    I have another question: How do you specify to which file the app should connect?
    Application is connecting to the database not to the file. You must define database name with QSqlDatabase::setDatabaseName method.

  7. #7
    Join Date
    Apr 2012
    Location
    Romania
    Posts
    22
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: cannot open include file qsqldatabase

    Ok. But how does the application know in which file to search the database I specified?

  8. #8
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: cannot open include file qsqldatabase

    Quote Originally Posted by Saurian View Post
    Ok. But how does the application know in which file to search the database I specified?
    Did you read my previous comment ?
    And i guess you mean in which location or directory , as Lesiok said its not a file , its a database , i told you 2 ways to do that.
    Either you specify the server name and database name your connecting to or you create a DSN
    Please read my previous post it clearly explains how to do that almost step by step.
    Hope this helps.

  9. #9
    Join Date
    Apr 2012
    Location
    Romania
    Posts
    22
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: cannot open include file qsqldatabase

    Quote Originally Posted by toufic.dbouk View Post
    Did you read my previous comment ?
    And i guess you mean in which location or directory , as Lesiok said its not a file , its a database , i told you 2 ways to do that.
    Either you specify the server name and database name your connecting to or you create a DSN
    Please read my previous post it clearly explains how to do that almost step by step.
    Hope this helps.
    Yes, I mean in which location. I want to be able to install this application on any PC and to work there. I want to compile it in Ubuntu too and be able there to make the application work. This is why I would need to know the location so I could zip the database with my application and all it's dll's.

    I've wrote the next code:

    Qt Code:
    1. QSqlDatabase db(QSqlDatabase::addDatabase("QMYSQL"));
    2.  
    3. //connect to database
    4. db.setHostName("localhost");
    5. db.setUserName("username");
    6. db.setPassword("password");
    7. db.setDatabaseName("database");
    8.  
    9.  
    10. if ( !db.open() )
    11. QMessageBox::critical(0, "Error", db.lastError().text());
    12. else
    13. QMessageBox::information(0, "OK", "Merge!!!");
    To copy to clipboard, switch view to plain text mode 

    and I get the following error box:

    "QSqlDatabase: QMYSQL driver not loaded
    QSqlDatabase: available drivers: QSQLITE QODBC QODBC3 QPSQL QPSQL7"

    I've tried to copy libmysql.dll to the folder where my .exe file is and to my qtcreator.exe folder and it's still not working

  10. #10
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: cannot open include file qsqldatabase

    Quote Originally Posted by Saurian
    Yes, I mean in which location.
    it depends on what SQL management application your using. Check your applications guide to find out the location.
    Quote Originally Posted by Saurian
    YI want to be able to install this application on any PC and to work there. I want to compile it in Ubuntu too and be able there to make the application work.
    im not sure of this , but i would say you have to place the database in your project and specify the path for it
    Qt Code:
    1. QString applicationPath= QApplication::applicationDirPath();/*get the application path*/
    To copy to clipboard, switch view to plain text mode 
    then append a QDir::separator() , add the database name , and set it to the setDatabaseName(/*QString representing that database path*/)

    I've tried to copy libmysql.dll to the folder where my .exe file is and to my qtcreator.exe folder and it's still not working
    place the .dll file in plugins/sqldrivers directory.
    you might need more than the .dll file , take a look at this thread http://qt-project.org/forums/viewthread/19122.
    you can also try to load QODBC driver since it more of a general driver.
    That's my suggestion , but ill wait with your for a reply from a more experienced developer to learn something new.
    Last edited by toufic.dbouk; 23rd September 2013 at 17:33.

  11. #11
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: cannot open include file qsqldatabase

    Quote Originally Posted by Saurian View Post
    Yes, I mean in which location. I want to be able to install this application on any PC and to work there. I want to compile it in Ubuntu too and be able there to make the application work. This is why I would need to know the location so I could zip the database with my application and all it's dll's.

    I've wrote the next code:

    Qt Code:
    1. QSqlDatabase db(QSqlDatabase::addDatabase("QMYSQL"));
    2.  
    3. //connect to database
    4. db.setHostName("localhost");
    5. db.setUserName("username");
    6. db.setPassword("password");
    7. db.setDatabaseName("database");
    8.  
    9.  
    10. if ( !db.open() )
    11. QMessageBox::critical(0, "Error", db.lastError().text());
    12. else
    13. QMessageBox::information(0, "OK", "Merge!!!");
    To copy to clipboard, switch view to plain text mode 

    and I get the following error box:

    "QSqlDatabase: QMYSQL driver not loaded
    QSqlDatabase: available drivers: QSQLITE QODBC QODBC3 QPSQL QPSQL7"

    I've tried to copy libmysql.dll to the folder where my .exe file is and to my qtcreator.exe folder and it's still not working
    First of all as I see You try to work with MySQL. To work with MySQL database You must install MySQL server and connect to them. Qt application is only a client. If You need to work with small database distributed with application use SQLite.
    Second, standard Qt distribution don't contain MySQL driver. You have to build it yourself.

Similar Threads

  1. Replies: 1
    Last Post: 23rd May 2011, 04:53
  2. Replies: 3
    Last Post: 1st November 2010, 16:33
  3. Replies: 4
    Last Post: 9th May 2010, 16:18
  4. Replies: 2
    Last Post: 9th March 2010, 05:21
  5. Replies: 10
    Last Post: 15th June 2009, 19:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.