View Full Version : Access an "embedded" MySQL-Database

18th February 2006, 09:47

I have MySQL4.* and Apache2 running and have created a database called 'qttest' with a table 'test'. All files are locatet at

root@***:/var/lib/mysql/qttest# ll
insgesamt 24
-rw-rw---- 1 mysql mysql 65 2006-02-18 10:18 db.opt
-rw-rw---- 1 mysql mysql 8578 2006-02-18 10:19 test.frm
-rw-rw---- 1 mysql mysql 40 2006-02-18 10:19 test.MYD
-rw-rw---- 1 mysql mysql 1024 2006-02-18 10:19 test.MYI
So if I connet in Qt via the localhost of my running Apache all works fine.

QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
bool ok = db.open();
[... SELECT ...]
But how can I connect to the qttest.test table if no Apache and MySQL is running? What value I need for db.setHostName, and how tell I Qt to use the files in /var/lib/mysql/qttest?


P.s.: SQLite is no alternative for me.

18th February 2006, 16:43
But how can I connect to the qttest.test table if no Apache and MySQL is running?

check Open()

19th February 2006, 06:22
check Open()
If I understand you right, I shuold read http://doc.trolltech.com/4.0/qsqldatabase.html#open, but I don't find things, that could help me.

Just as well checking the output of lastError() dosen't help me much, because I know, that using QSqlDatabase::setHostName with the local dirpath produceses "Unknown MySQL Server Host '/var/lib/mysql/qttest'", but if I leave empty, it takes localhost, and without running mysql-server there is the warning, that it can't connect via socket...

Also the dirpath/file in QSqlDatabase::setDatabaseName dosen't work, as with SQLite.

So could you please give me some more information?



20th February 2006, 12:12
As far as I know mysql always works in client-server mode... for using an embedded db engine you're stuck with one thats meant to operate in such a way like sqlite.

mysql does support both unix sockets and tcp connections, but it's always gonna use some sort of transport layer to the mysqld

20th February 2006, 13:03
mysql does support both unix sockets and tcp connections, but it's always gonna use some sort of transport layer to the mysqld

That means, that I have to deliver MySQL with my programm, and start the MySQL-Server-Engine or what ever in main() to be abel to connect with my Databases at later time.

Pfff, then I better use my time to try to get REGEXP run in SQLite with QT.


20th February 2006, 13:15
Oh .....
MySql must be run in server!
And then your qt program via QSqlDatabase can conect to your db!

20th February 2006, 13:46
You can run an embedded mysql server in your application. You need to link with "-lmysqld".

More info on the subject:

20th February 2006, 15:17

I already saw these urls and read it, but still what values do I need then for setHostName() and setDatabaseName(), when I want use provided tables (e.g. /home/me/db/testtable.frm, and the *MYD *MYI)?

Thanks for helping


20th February 2006, 15:24
If you use an embedded dbms, you can probably leave the fields empty.

Here are some examples on using the embedded mysql server (http://dev.mysql.com/doc/refman/4.1/en/libmysqld-example.html). The connection strings should be directly usable in Qt routines too (they use NULLs, so it's equal to leaving the connection fields blank). The database name is the database name of your database :) Just remember to set up the dbms properly (to use the database files of your choice).

21st February 2006, 07:41
Well at this time I yet have problems bilding QT with mysqld. Because I want not build a plugin, so that I can compile my program static. (Even if this is possible!?)

What I have done is Downloading the sources of Qt4.1 and MySQL4.* and have configured Qt with:

./configure -v -release -fast -static -qt-sql-mysql -qt-sql-sqlite -I/usr/include/mysql/ -L/usr/local/lib -lmysqld

But I saw that ./configure outputs:

qmake vars ...... [...] "CONFIG += use_libmysqlclient_r"
[... but also ...] "LIBS+= -lmysqld"

So I hoped it will work anyway (even when there is the libmysql-flag), so 2h later I saw: it dosn't :mad:

It still uses my local MySQL-server. So my big Question:

Is it possible to compile Qt static with the embedded MySQL-driver mysqld?


21st February 2006, 10:28
It is not Qt which needs to be linked with the embedded server but rather your application. Qt serves only as a client.