Basic qt with mysql?
-
wrote on 10 Oct 2010, 10:45 last edited by
hi
i install qt creator then mysql driver for qt and then i install xampp to make databases for qt. Now i want to access data in database using qt. i am following this link but it is not working for me. "Your text to link here...":http://doc.trolltech.com/3.0/sql.html
then problem is when i type the same code
@
#include <QApplication>
#include <QSqlDatabase>int main( int argc, char *argv[] ) { QApplication app( argc, argv ); QSqlDatabase *defaultDB = QSqlDatabase::addDatabase( QMYSQL ); if ( ! defaultDB ) { qWarning( "Failed to connect to the database driver" ); return 1; } defaultDB->setDatabaseName( "testTwo" ); defaultDB->setUserName( "root" ); defaultDB->setPassword( "" ); defaultDB->setHostName( "localhost" ); if ( defaultDB->open() ) { // Database successfully opened; we can now issue SQL commands. } return 0; }
@
then it returns these errors
@
QSqlDatabase: No such file or directory main.cpp 2
QSqlDatabase was not declare in this scope main.cpp 9
defaultDB was not declare in this scope main.cpp 9
QSqlDatabase is not a class or namespace main.cpp 9
QMYSQL was not declare in this scope main.cpp 9
@how can i access data from my database table?
other thing is i am using xampp for creating mysql databases. when i make database it is placed in this directory opt/lampp/var/mysql/testTwo. -
wrote on 10 Oct 2010, 10:56 last edited by
now it displays this error
@
in function int main(int, char**)
cannot convert to QSqlDatabase to QSqlDatabase in inititalization main.cpp 9
@ -
wrote on 10 Oct 2010, 11:26 last edited by
That's because you assign QSqlDatabase *defaultDB ( a pointer) with what "addDatabase(...)":http://doc.qt.nokia.com/4.6/qsqldatabase.html#addDatabase returns (a QSqlDatabase, not a pointer to QSqlDatabase)
-
wrote on 10 Oct 2010, 11:29 last edited by
so i should use something like this QSqlDatabase defaultDB = QSqlDatabase::addDatabase("QMYSQL");
-
wrote on 10 Oct 2010, 11:30 last edited by
Yes. And in case of one db without name (i.e. default database) you don't need to provide it to QSqlQuery.
-
wrote on 10 Oct 2010, 11:34 last edited by
now it displays this error
@
in function int main(int, chat**)
no match for operator! in !db
candidates are operator!(bool) <built-in>
@ -
wrote on 10 Oct 2010, 11:54 last edited by
You can use "isValid()":http://doc.qt.nokia.com/4.6/qsqldatabase.html#isValid
-
wrote on 10 Oct 2010, 11:58 last edited by
now it is saying
@
QSqlDatabase: QMysql driver is not loaded
QSqlDatabase: available drivers :QSQLITE QSQLITE2
@i have installed Mysql driver here "Your text to link here...":http://developer.qt.nokia.com/forums/viewthread/1121/P30/
-
wrote on 10 Oct 2010, 16:23 last edited by
It says that something is wrong with your mysql plugin. What do you have in your plugins/sqldrivers folder?
-
wrote on 10 Oct 2010, 16:35 last edited by
i have 10 different folders and four files. files with names are libqsqlmysql.so, qsqldriverbase.pri, readme, sqldriver.pro
-
wrote on 10 Oct 2010, 16:37 last edited by
i also run this code
@
#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlError>
#include <QStringList>
#include <QtDebug>int main( int argc, char *argv ) { QCoreApplication app( argc, argv ); qDebug() << QSqlDatabase::drivers(); }
@
it displays this
@
("QSQLITE","QSQLITE2")
@ -
wrote on 10 Oct 2010, 17:25 last edited by
Not folder with plugin sources, but folder with qt plugin binaries. It is folder named plugins/sqldrivers in root of your Qt folder.
-
wrote on 10 Oct 2010, 17:42 last edited by
well i checked this in this path home/qtsdk-2010.05/qt/src/plugins/sqldrivers
I have two plugins folders. one in home/qtsdk-2010.05/qt/plugins
in this plugin folder i have 11 folders with one named sqldrivers. inside this sqldriver folder i have these 3 files "libqsqlite.so, libqsqlite2.so, libqsqlpsql.so"the other is mentioned above which inside src.
-
wrote on 10 Oct 2010, 18:15 last edited by
Put mysql plugin in this folder.
-
wrote on 10 Oct 2010, 18:17 last edited by
from which folder i can get mysql plugin and what is the name of that mysql plugin? you mean this one libqsqlmysql.so which is in src/plugins/sqldrivers/mysql folder?
-
wrote on 10 Oct 2010, 18:28 last edited by
yes
1/17