October 10, 2010

doforumda doforumda
Lab Rat
161 posts

basic qt with mysql?

Page  
1

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… [doc.trolltech.com]

then problem is when i type the same code

  1.     #include <QApplication>
  2.     #include <QSqlDatabase>
  3.  
  4.     int main( int argc, char *argv[] )
  5.     {
  6.         QApplication app( argc, argv );
  7.  
  8.         QSqlDatabase *defaultDB = QSqlDatabase::addDatabase( QMYSQL );
  9.         if ( ! defaultDB ) {
  10.             qWarning( "Failed to connect to the database driver" );
  11.             return 1;
  12.         }
  13.         defaultDB->setDatabaseName( "testTwo" );
  14.         defaultDB->setUserName( "root" );
  15.         defaultDB->setPassword( "" );
  16.         defaultDB->setHostName( "localhost" );
  17.  
  18.         if ( defaultDB->open() ) {
  19.             // Database successfully opened; we can now issue SQL commands.
  20.         }
  21.  
  22.         return 0;
  23.     }

then it returns these errors

  1. QSqlDatabase: No such file or directory         main.cpp    2
  2. QSqlDatabase was not declare in this scope   main.cpp    9
  3. defaultDB was not declare in this scope         main.cpp 9
  4. QSqlDatabase is not a class or namespace       main.cpp 9
  5. 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.

16 replies

October 10, 2010

Vass Vass
Ant Farmer
740 posts

in .pro file you should add

  1. QT += sql

 Signature 


Vasiliy

October 10, 2010

doforumda doforumda
Lab Rat
161 posts

now it displays this error

  1. in function int main(int, char**)
  2. cannot convert to  QSqlDatabase to QSqlDatabase in inititalization       main.cpp 9

October 10, 2010

Zlatomir Zlatomir
Hobby Entomologist
361 posts

That’s because you assign QSqlDatabase *defaultDB ( a pointer) with what addDatabase [doc.qt.nokia.com] returns (a QSqlDatabase, not a pointer to QSqlDatabase)

October 10, 2010

doforumda doforumda
Lab Rat
161 posts

so i should use something like this QSqlDatabase defaultDB = QSqlDatabase::addDatabase(“QMYSQL”);

October 10, 2010

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

Yes. And in case of one db without name (i.e. default database) you don’t need to provide it to QSqlQuery.

October 10, 2010

doforumda doforumda
Lab Rat
161 posts

now it displays this error

  1. in function int main(int, chat**)
  2. no match for operator! in !db
  3. candidates are operator!(bool) <built-in>

October 10, 2010

Zlatomir Zlatomir
Hobby Entomologist
361 posts

You can use isValid() [doc.qt.nokia.com]

October 10, 2010

doforumda doforumda
Lab Rat
161 posts

now it is saying

  1. QSqlDatabase: QMysql driver is not loaded
  2. QSqlDatabase: available drivers :QSQLITE QSQLITE2

i have installed Mysql driver here Your text to link here… [developer.qt.nokia.com]

October 10, 2010

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

It says that something is wrong with your mysql plugin. What do you have in your plugins/sqldrivers folder?

October 10, 2010

doforumda doforumda
Lab Rat
161 posts

i have 10 different folders and four files. files with names are libqsqlmysql.so, qsqldriverbase.pri, readme, sqldriver.pro

October 10, 2010

doforumda doforumda
Lab Rat
161 posts

i also run this code

  1.       #include <QCoreApplication>
  2.       #include <QSqlDatabase>
  3.       #include <QSqlError>
  4.       #include <QStringList>
  5.       #include <QtDebug>
  6.  
  7.       int main( int argc, char *argv )
  8.       {
  9.       QCoreApplication app( argc, argv );
  10.       qDebug() << QSqlDatabase::drivers();
  11.       }

it displays this

  1. ("QSQLITE","QSQLITE2")

October 10, 2010

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

Not folder with plugin sources, but folder with qt plugin binaries. It is folder named plugins/sqldrivers in root of your Qt folder.

October 10, 2010

doforumda doforumda
Lab Rat
161 posts

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.

October 10, 2010

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

Put mysql plugin in this folder.

October 10, 2010

doforumda doforumda
Lab Rat
161 posts

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?

Page  
1

  ‹‹ Problem building Qt 4.7 with QMySql driver.      [Solved] Big performance issue with TreeView ››

You must log in to post a reply. Not a member yet? Register here!