PDA

View Full Version : cannot open include file qsqldatabase



Saurian
22nd September 2013, 20:24
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:



QSqlDatabase db(QSqlDatabase::addDatabase("QMYSQL"));

//connect to database
db.setDatabaseName("sarbatori.mwb");

if ( !db.open() )
QMessageBox::critical(0, "Error", db.lastError().text());
else
QMessageBox::information(0, "OK", "Merge!!!");

toufic.dbouk
22nd September 2013, 22:29
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 :

QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
QString servername = "X-PC\\LOCALHOST";
QString dbname = "CARSQL";
QString connTemplate = "DRIVER={SQL SERVER};SERVER=%1;DATABASE=%2;";
QString connectionString = connTemplate.arg(servername).arg(dbname);
db.setDatabaseName(connectionString);

or


QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
db.setHostName("acidalia");
db.setDatabaseName("customdb");
db.setUserName("mojito");
db.setPassword("J0a1m8");

ChrisW67
22nd September 2013, 22:53
I've already put the line in the .pro file: QT += sql
Has anyone any ideas what can I do?
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.

Saurian
23rd September 2013, 07:56
Yes, run qmake after changing the PRO file.


Thanks. It works. :)



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:




just a note, dont you need a connection string or a dsn to open the connection with a database ?
something like :

QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
QString servername = "X-PC\\LOCALHOST";
QString dbname = "CARSQL";
QString connTemplate = "DRIVER={SQL SERVER};SERVER=%1;DATABASE=%2;";
QString connectionString = connTemplate.arg(servername).arg(dbname);
db.setDatabaseName(connectionString);

or


QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
db.setHostName("acidalia");
db.setDatabaseName("customdb");
db.setUserName("mojito");
db.setPassword("J0a1m8");

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?

toufic.dbouk
23rd September 2013, 10:39
QString dbname = "CARSQL"; /* thats the name of your created database */

in case you want to use a dsn, you can set the database name in the options
for windows:open 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:

QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("yourDSN");


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

Lesiok
23rd September 2013, 10:58
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.

Saurian
23rd September 2013, 13:05
Ok. But how does the application know in which file to search the database I specified?

toufic.dbouk
23rd September 2013, 14:15
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.

Saurian
23rd September 2013, 15:54
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:



QSqlDatabase db(QSqlDatabase::addDatabase("QMYSQL"));

//connect to database
db.setHostName("localhost");
db.setUserName("username");
db.setPassword("password");
db.setDatabaseName("database");


if ( !db.open() )
QMessageBox::critical(0, "Error", db.lastError().text());
else
QMessageBox::information(0, "OK", "Merge!!!");


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

toufic.dbouk
23rd September 2013, 17:16
Yes, I mean in which location.
it depends on what SQL management application your using. Check your applications guide to find out the location.

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

QString applicationPath= QApplication::applicationDirPath();/*get the application path*/
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.

Lesiok
23rd September 2013, 17:50
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:



QSqlDatabase db(QSqlDatabase::addDatabase("QMYSQL"));

//connect to database
db.setHostName("localhost");
db.setUserName("username");
db.setPassword("password");
db.setDatabaseName("database");


if ( !db.open() )
QMessageBox::critical(0, "Error", db.lastError().text());
else
QMessageBox::information(0, "OK", "Merge!!!");


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.