Basic qt with mysql?
-
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. -
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)
-
Yes. And in case of one db without name (i.e. default database) you don't need to provide it to QSqlQuery.
-
You can use "isValid()":http://doc.qt.nokia.com/4.6/qsqldatabase.html#isValid
-
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/
-
It says that something is wrong with your mysql plugin. What do you have in your plugins/sqldrivers folder?
-
i have 10 different folders and four files. files with names are libqsqlmysql.so, qsqldriverbase.pri, readme, sqldriver.pro
-
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")
@ -
Not folder with plugin sources, but folder with qt plugin binaries. It is folder named plugins/sqldrivers in root of your Qt folder.
-
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.
-
Put mysql plugin in this folder.
-
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?
-
yes