Results 1 to 9 of 9

Thread: Class to connect and make a query on a database

  1. #1
    Join Date
    Oct 2009
    Location
    Rio de Janeiro - Brazil
    Posts
    19
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Class to connect and make a query on a database

    Hi everyone,

    I'd like to create a simple class to run my queries in my program.

    The sctructure is:

    ---------------------------------------------
    ::: libdb.h
    ---------------------------------------------

    Qt Code:
    1. #ifndef LIBDB_H
    2. #define LIBDB_H
    3.  
    4. #include <QSqlDatabase>
    5. #include <QSqlQuery>
    6. #include <QString>
    7. #include <QDebug>
    8.  
    9. class DataBase
    10. {
    11. public:
    12. DataBase();
    13. virtual ~DataBase();
    14. QSqlQuery setQuery(QString);
    15. };
    16.  
    17. #endif // LIBDB_H
    To copy to clipboard, switch view to plain text mode 

    ---------------------------------------------
    ::: libdb.cpp
    ---------------------------------------------

    Qt Code:
    1. #include "libdb.h"
    2.  
    3. DataBase::DataBase()
    4. {
    5. }
    6.  
    7. DataBase::~DataBase()
    8. {
    9. }
    10.  
    11. QSqlQuery setQuery(QString sql_statement)
    12. {
    13. QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
    14. db.setHostName("10.0.2.2");
    15. db.setDatabaseName("testdb");
    16. db.setUserName("root");
    17. db.setPassword("password");
    18. if (!db.open()) {
    19. qDebug() << "database wasn't opened";
    20. return NULL;
    21. } else {
    22. QSqlQuery sqlquery(sql_statement, db);
    23. sqlquery.prepare(sql_statement);
    24. sqlquery.exec();
    25. db.commit();
    26. db.close();
    27. return sqlquery;
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 

    ---------------------------------------------
    ::: mainwindow.cpp
    ---------------------------------------------

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "libdb.h"
    4.  
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10.  
    11. DataBase mDB = DataBase();
    12. QSqlQuery query;
    13. query = mDB.setQuery("SELECT * FROM alarm");
    14. while(query.next()) {
    15. QString alarm = query.value(0).toString();
    16. qDebug() << "Alarms: " << alarm;
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    But, I've gotting the error:
    undefined reference to `Database::setQuery(QString)' mainwindow.cpp13
    mainwindow.cpp13 = 'query = mDB.setQuery("SELECT * FROM alarm");'

    What I'm doing wrong?

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Class to connect and make a query on a database

    Easy:

    QSqlQuery setQuery(QString sql_statement)

    should be:

    QSqlQuery Database::setQuery(QString sql_statement)

  3. #3
    Join Date
    Oct 2009
    Location
    Rio de Janeiro - Brazil
    Posts
    19
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Class to connect and make a query on a database

    Thanks tbscope, now it can compile, but, now I've got another error:

    QSqlDatabase: QPSQL driver not loaded
    QSqlDatabase: available drivers: QSQLITE QODBC3 QODBC
    How can I install QPSQL driver?

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Class to connect and make a query on a database

    Quote Originally Posted by junix View Post
    How can I install QPSQL driver?
    By reading the documentation! See "SQL Database Drivers". And it is a bad idea to set up a database every time DataBase::setQuery() is called. Qt stores a connection, so there is no need to set it up again and again.

  5. #5
    Join Date
    Oct 2009
    Location
    Rio de Janeiro - Brazil
    Posts
    19
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Class to connect and make a query on a database

    Ok.
    I'm downloading the PostgreSQL for Windows to compile the plugin.

    So you think that is necessary only once to initialize QSqlDatabase?

    The class should be only to make queries? In this case I can not close the connection, right?

    db.close();

  6. #6
    Join Date
    Oct 2009
    Location
    Rio de Janeiro - Brazil
    Posts
    19
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Class to connect and make a query on a database

    I've tryed to build QPSQL driver, but... I'm using MinGW.
    Following the QT Wiki documentation, I downloaded all of MinGW-Tools package and there is no reimp tool inside!
    I found a link to reimp tool made by Anders Norlander, but his site is off.
    After some searchs, I found another guy called Jose Fonseca and I got the reimp tool, but only sources files that I couldn't build.

    Maybe there is another way to build or download QPSQL driver?

    Somebody has the driver and can put available to download?

    Thanks.

  7. #7
    Join Date
    Oct 2009
    Location
    Rio de Janeiro - Brazil
    Posts
    19
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Class to connect and make a query on a database

    Finnaly after 8 hours trying to build this driver, it's done!
    On Windows 7 it is impossible! Only it's works on XP.
    I build on XP and copy to Win 7.

    Can I share this .dll with another people?

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Class to connect and make a query on a database

    Quote Originally Posted by junix View Post
    Can I share this .dll with another people?
    I don't know. Try if it works. E.g. set up a virtual box or simply try it on an other computer.
    As for the database question. Of course you then shouldn't close it. see QSqlDatabase::addDatabase() and it second parameter! and for the query simply use
    Qt Code:
    1. QSqlQuery q(QSqlDatabase::database("YourConnectionName"));
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Oct 2009
    Location
    Rio de Janeiro - Brazil
    Posts
    19
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Class to connect and make a query on a database

    Yes, it works!
    I build on XP and copy to Windows 7 and both of them is working.
    I'd like to share this dll's, if somebody knows if is possible, please, tell me.

    Thanks.

Similar Threads

  1. QSqlDatabase - How to connect to multiple database?
    By cutie.monkey in forum Qt Programming
    Replies: 4
    Last Post: 10th March 2010, 12:03
  2. How to realize multi-threaded database query?
    By bangqianchen in forum Qt Programming
    Replies: 2
    Last Post: 14th November 2008, 07:15
  3. Replies: 12
    Last Post: 18th September 2008, 15:04
  4. [QT4][SQLITE] Database and query
    By agent007se in forum Newbie
    Replies: 10
    Last Post: 12th July 2006, 22:16
  5. My application can't connect to database when deploy it???
    By gtthang in forum Installation and Deployment
    Replies: 1
    Last Post: 15th February 2006, 11:01

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.