Qt-interest Archive, August 2007
(macosX,Qt4.3.1devel+src,eclipse europa) impossible to connect to the database via Qt
Message 1 in thread
Hello,
I would like to connect to a database with Qt, but I have the following
error message with this program:
#include <QtSql>
#include <QApplication>
#include <QtGui>
int main(int argc,char* argv[]){
QApplication app(argc,argv);
QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("mysql");
db.setUserName("root");
db.setPassword("myPassword");
if (!db.open())
QMessageBox::critical(0,"Database error",db.lastError().text());
else
QMessageBox::information(0,"connexion à la base","OK");
return app.exec();
}
I specify that the parameters are good, I successfully connect to the
database with navicat.
And I have used the following parameters to configure Qt: "./configure
-plugin-sql-mysql".
Am I obliged to do some specific thing to use mysql ?
Thank you,
lolveley.
Message 2 in thread
I complete my previous post: I have found this text on the Qt site (I am
not very sure if this content is copyrighted, I try and I will see...),
it is about the installation of mysql support, the most interesting is
the last paragraph.
???????????????????
Supported Databases
The table below lists the drivers included with Qt. Due to license
incompatibilities with the GPL, not all of the plugins are provided with
the Qt Open Source Edition.
Driver name DBMS
QDB2 <http://doc.trolltech.com/4.2/sql-driver.html#qdb2> IBM DB2
(version 7.1 and above)
QIBASE <http://doc.trolltech.com/4.2/sql-driver.html#qibase> Borland
InterBase
QMYSQL <http://doc.trolltech.com/4.2/sql-driver.html#qmysql> MySQL
QOCI <http://doc.trolltech.com/4.2/sql-driver.html#qoci> Oracle Call
Interface Driver
QODBC <http://doc.trolltech.com/4.2/sql-driver.html#qodbc> Open
Database Connectivity (ODBC) - Microsoft SQL Server and other
ODBC-compliant databases
QPSQL <http://doc.trolltech.com/4.2/sql-driver.html#qpsql> PostgreSQL
(versions 7.3 and above)
QSQLITE2 <http://doc.trolltech.com/4.2/sql-driver.html#qsqlite2> SQLite
<http://doc.trolltech.com/4.2/3rdparty.html#sqlite> version 2
QSQLITE <http://doc.trolltech.com/4.2/sql-driver.html#qsqlite> SQLite
<http://doc.trolltech.com/4.2/3rdparty.html#sqlite> version 3
QTDS <http://doc.trolltech.com/4.2/sql-driver.html#qtds> Sybase
Adaptive Server
*Note:* To build a driver plugin you need to have the appropriate client
library for your Database Management System (DBMS). This provides access
to the API exposed by the DBMS, and is typically shipped with it. Most
installation programs also allow you to install "development libraries",
and these are what you need. These libraries are responsible for the
low-level communication with the DBMS.
Building the Drivers Using Configure
On Unix and Mac OS X, the Qt configure script tries to automatically
detect the available client libraries on your machine. Run configure
-help to see what drivers can be built. You should get an output similar
to this:
-no-sql-<driver> ... Disable SQL <driver> entirely.
-qt-sql-<driver> ... Enable a SQL <driver> in the Qt Library, by default
none are turned on.
-plugin-sql-<driver> Enable SQL <driver> as a plugin to be linked to
at run time.
Possible values for <driver>:
[ db2 ibase mysql oci odbc psql sqlite sqlite2 tds ]
The configure script cannot detect the neccessary libraries and include
files if they are not in the standard paths, so it may be necessary to
specify these paths using the -I and -L command-line options. For
example, if your MySQL include files are installed in /usr/local/mysql
(or in C:\mysql\include on Windows), then pass the following parameter
to configure: -I/usr/local/mysql (or -I C:\mysql\include for Windows).
On Windows the -I parameter doesn't accept spaces in filenames, so use
the 8.3 name instead; for example, use C:\progra~1\mysql instead of
C:\Program Files\mysql.
Use the -qt-sql-<driver> parameter to build the database driver
statically into your Qt library or -plugin-sql-<driver> to build the
driver as a plugin. Look at the sections that follow for additional
information about required libraries.
Building the Plugins Manually
QMYSQL for MySQL 4 and higher
Stored Procedure Support
MySQL 5 introduces stored procedure support at the SQL level, but no API
to control IN, OUT and INOUT parameters. Therefore, parameters have to
be set and read using SQL commands instead of QSqlQuery::bindValue
<http://doc.trolltech.com/4.2/qsqlquery.html#bindValue>().
Example stored procedure:
create procedure qtestproc (OUT param1 INT, OUT param2 INT)
BEGIN
set param1 = 42;
set param2 = 43;
END
Source code to access the OUT values:
QSqlQuery q;
q.exec("call qtestproc (@outval1, @outval2)");
q.exec("select @outval1, @outval2");
q.next();
qDebug() << q.value(0) << q.value(1); // outputs "42" and "43"
*Note:* @outval1 and @outval2 are variables local to the current
connection and will not be affected by queries sent from another host or
connection.
Embedded MySQL Server
The MySQL embedded server is a drop-in replacement for the normal client
library. With the embedded MySQL server, a MySQL server is not required
to use MySQL functionality.
To use the embedded MySQL server, simply link the Qt plugin to libmysqld
instead of libmysqlclient. This can be done by replacing -lmysqlclient_r
by -lmysqld in the qmake command in the section below.
Please refer to the MySQL documentation, chapter "libmysqld, the
Embedded MySQL Server Library" for more information about the MySQL
embedded server.
How to Build the Plugin on Unix and Mac OS X
You need the MySQL header files and as well as the shared library
libmysqlclient.so. Depending on your Linux distribution you may need to
install a package which is usually called "mysql-devel".
Tell qmake <http://doc.trolltech.com/4.2/qmake-manual.html#qmake> where
to find the MySQL header files and shared libraries (here it is assumed
that MySQL is installed in /usr/local) and run make:
cd $QTDIR/src/plugins/sqldrivers/mysql
qmake -o Makefile "INCLUDEPATH+=/usr/local/include" "LIBS+=-L/usr/local/lib -lmysqlclient_r" mysql.pro
make
After installing Qt, as described in the Installing Qt/X11
<http://doc.trolltech.com/4.2/install-x11.html> document, you also need
to install the plugin in the standard location:
cd $QTDIR/src/plugins/sqldrivers/mysql
make install
?????????????????????
I must acknowledge I have not understood this last paragraph (how to
build the plugin...) : if we use qmake, it means that Qt is already
installed, so we would have to re-install qt in order to use mysql??
Could someone give me the way to have the mysql driver run , knowing
that I have installed mysql,then Qt-devel4.3.1, then Qt-src-4.3.1?
Regards,
lolveley.
lolveley a écrit :
> Hello,
>
> I would like to connect to a database with Qt, but I have the
> following error message with this program:
>
> #include <QtSql>
> #include <QApplication>
> #include <QtGui>
>
>
> int main(int argc,char* argv[]){
> QApplication app(argc,argv);
>
>
> QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
> db.setHostName("localhost");
> db.setDatabaseName("mysql");
> db.setUserName("root");
> db.setPassword("myPassword");
> if (!db.open())
> QMessageBox::critical(0,"Database error",db.lastError().text());
> else
> QMessageBox::information(0,"connexion à la base","OK");
>
>
>
>
> return app.exec();
>
> }
>
> I specify that the parameters are good, I successfully connect to the
> database with navicat.
>
> And I have used the following parameters to configure Qt: "./configure
> -plugin-sql-mysql".
> Am I obliged to do some specific thing to use mysql ?
>
> Thank you,
>
>
> lolveley.
>
>
>
>
>
>
Message 3 in thread
To complete the two previous posts, here is the copy of some letters I
found in the qt mailing-list archives:
??????????????????????
Qt-interest Archive, January 2005
compiling mysql-plugin
------------------------------------------------------------------------
Message 1 in thread
* /Subject/: compiling mysql-plugin
* /From/: "Heinz A. Krebs" <heinz@xxxxxxxxx
<mailto:heinz@xxxxxxxxxxxxx>>
* /Date/: Mon, 10 Jan 2005 09:04:28 +0100
* /To/: qt-interest@xxxxxxxxxxxxx <mailto:qt-interest@xxxxxxxxxxxxx>
servas!
i'm trying to install mythtv on my box, therefor i need the mysql-
plugin; but have no success:
1.______________
qmake -o Makefile "INCLUDEPATH+=/usr/local/include" "LIBS+=-
L/usr/local/lib" mysql.pro && make
gives:
../../../../src/sql/drivers/mysql/qsql_mysql.h:49:19: mysql.h: Datei
oder Verzeichnis nicht gefunden
o.k., my mysql header and include-files are in the
subdir /usr/local/include/mysql
2.______________
qmake -o Makefile "INCLUDEPATH+=/usr/local/include/mysql" "LIBS+=-
L/usr/local/lib/mysql" mysql.pro && make
results in correct compilation, but if i start mythvt i get the
response:
mythtv: relocation error: /usr/src/qt-x11-
free-3.3.3/plugins/sqldrivers/libqsqlmysql.so: undefined symbol:
mysql_server_init
3._______________
qmake -o Makefile "INCLUDEPATH+=/usr/local/include/mysql" "LIBS+=-
L/usr/local/lib/mysql -lmysqlclient" mysql.pro && make
results also in correct compilation, but this time mythtv's output is:
QSqlDatabase: QMYSQL3 driver not loaded
QSqlDatabase: available drivers:
QSqlDatabase: QMYSQL3 driver not loaded
QSqlDatabase: available drivers:
Unable to connect to database!
No error type from QSqlError? Strange...
couldn't open db
no available drivers? but in the
directory /usr/local/qt/plugins/sqldrivers is the file libqsqlmysql.so
(and i did a make uninstall before every qmake!)
what am i doing wrong??????
qt-x11-free-3.3.3
mysql-4.1.8a
mythtv-0.16
ciao
H.
--
[ signature omitted ]