PDA

View Full Version : I want to use QMYSQL



gt.beta2
1st April 2009, 15:34
Hi.

I tried a tipical:

bool MainWindow::connecToDB(){
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("http://localhost");
db.setDatabaseName("name");
db.setUserName("user");
db.setPassword("password");
if(!db.open()){
QMessageBox msgBox;
msgBox.setModal(true);
msgBox.setText("Error ...");
msgBox.exec();
return false;
}
return true;
}

I came to the error:

QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QSQLITE2 QPSQL7 QPSQL

I suppose this has something to do with the license restrictions for driver included in Qt?

I am using Debian/GNU LInux and i installed Qt with this Source Package: qt4-x11 (4.4.3-1) (http://packages.debian.org/source/lenny/qt4-x11).

How do i install the driver?

Thanks

spirit
1st April 2009, 15:40
you have to build mysql plugin or rebuild Qt with support of mysql.
read this (http://doc.trolltech.com/4.5/sql-driver.html#qmysql-for-mysql-4-and-higher) for more info.

gt.beta2
1st April 2009, 16:55
Thanks for the info ... but now i realise my system is in a big mess!
I install something with apt, others with custom scrips (Qt Creator) and others by compiling the source code .... a big mess!
I will delete everything and install it all from source starting by this (http://doc.trolltech.com/4.5/install-x11.html).
Wish me luck :rolleyes:

spirit
1st April 2009, 16:56
it's not to hard. :) but good luck ;)

gt.beta2
3rd April 2009, 11:24
Hello!

Compiled Qt with:

./configure -qt-sql-mysql -dbus-linked -xshape
make
make install

Aparently it is working because i compiled some programs that i created before.

About the initial issue, the MySql connection ...

The initial example now gives me this message:

mainwindow.h:5:24: error: QSqlDatabase: No such file or directory

Whant include files will i need?

Thanks

spirit
3rd April 2009, 11:33
did you add


CONFIG += sql

line in your pro-file?
did you include #include <QSqlDatabase> or #include <QtSql> in sources?
last include, includes all sql module into your sources.

gt.beta2
3rd April 2009, 11:59
you were right about the

Quote:
CONFIG += sql

I had #include <QtSqlDatabse> an now changed to QtSql but get the same:

mainwindow.h:5:17: error: QtSql: No such file or directory


What am i missing here?!

gt.beta2
3rd April 2009, 12:02
got it ...
QT += sql :)

spirit
3rd April 2009, 12:40
oop, yes QT += sql, sorry for my mistake. just mistyped. :o

gt.beta2
3rd April 2009, 15:39
And now for the final question ... i hope.

I installed a tipical LAMP for testing the connection:

apt-get install apache2 mysql-server php5 phpmyadmin

When using http://localhost for the HostName i get:


Unknown MySQL server host 'http://localhost' (1) QMYSQL: Unable to connect


Now what :rolleyes:

JhonJames
3rd April 2009, 15:43
MySQL is running??

$ sudo /etc/init.d/mysql start

gt.beta2
3rd April 2009, 15:49
I can access to phpmyadmin ... so i supose yes.

JhonJames
3rd April 2009, 15:58
Try with:



db.setHostName("localhost");


Without "http://".

gt.beta2
3rd April 2009, 16:06
uff ... you were right :)

Thanks

graciano
15th April 2009, 14:13
Hello!
And if the database is in a server with ip 192.168.1.100?
I tried:


db.setHostName("192.168.1.100");


and it is not connecting.

Why is that? Must i give the port address or something?!

Thanks

spirit
15th April 2009, 16:08
try to set port. check you firewall.
can you connect to this address using Qt's example which is located in QTDIR/demos/sqlbrowser?