Results 1 to 8 of 8

Thread: Database connection

  1. #1
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Question Database connection

    I am having a problem making a database connection. I have tried several samples and turtorials to no avail. I am using QT Creator 2.0. I think I might have identified the problem but not sure how to fix it. I have a SQLite database named rider.mt and Iam trying to connect to it. I would eventually create a seperate class for the database operations but I want to get it figured out first. Below is the code and here are my issues:
    1. Is this the proper way to access the database?
    2. How would I indicate a path if I elect to have the database in a different directory.
    3. I get a build error that says QTSql no such file or directory.. I am sure this is the main problem but don't know how to fix it.

    Qt Code:
    1. #include <QtGui>
    2. #include <QtSql> //*** Error here
    3. #include "dbmain.h"
    4.  
    5. bool createConnection()
    6. {
    7. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    8. db.setDatabaseName("rider.mt");
    9. if (!db.open()) {
    10. QMessageBox::warning(0, QObject::tr("Database Error"),
    11. db.lastError().text());
    12. return false;
    13. }
    14. return true;
    15. }
    16.  
    17. int main(int argc, char *argv[])
    18. {
    19. QApplication a(argc, argv);
    20. DBMain w;
    21. w.show();
    22.  
    23. return a.exec();
    24. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for looking at this!

  2. #2
    Join Date
    Sep 2010
    Posts
    1
    Qt products
    Platforms
    Windows

    Default Re: Database connection

    Hi, I am new to techie database stuff. Can anybody help me out in getting familiar with the technical matters I have to know about all this thing?

  3. #3
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Database connection

    @poporacer
    The error is because You don't have
    QT += sql
    in Your .pro file.
    If You would read docs, You wouldn't have this problem. I mean... it's on the firs page of QSql module man (RTM!).

    @henzbelmont
    Hi, I am new to techie database stuff. Can anybody help me out in getting familiar with the technical matters I have to know about all this thing?
    No You don't have to know all about this thing . That's why ppl without any academics background build a space rockets/electron accelerators and work in NASA or CERN.
    Sorry for my sarcasm, I don't want to offend You, and I also don't know a lot of the things (i.e. my poor eng) ...
    But seriously, the more You know... the more You don't know .
    If you refer to the poporacer post as "this thing", then Yes, You have to know "this thing", because this is the way to open db, so If You don't know how to open db then how will You operate on it?

    I suggest reading some literature regarding functionality/usage of DB, there is plenty of good websites explaining how to retrieve data from DB, etc...
    Then, depending on Your programming/Qt knowledge, read about qsql module.

    Regards and best luck

  4. #4
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Database connection

    Talei,

    I could understand you being a smart a$$ if this was posted in another forum. This was posted in the Newbie forum and yes I have read the manual. However, this manual is several hundred pages and things can be missed. In addition, the manual is not clear for someone just getting started. I did add the QT += sql to the QMake file...and this did not work. Not being familiar with the program, I didn't realize that there were 2 qmake functionalities. Every problem is covered in the manual or documented elsewhere so I could become an expert and respond to every post and say "Read the Manual"!!!

  5. #5
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Database connection

    Sorry about that, I didn't wanted to sound like a smart ass, nor to offend You in any way, but answer to your question was in one of the 7 examples provided with Qt SDK. Also it's on the first page of the qsql module.
    But as You said I understand that for the new one to the Qt it may be somewhat confusing.
    Anyway I'm glad You figure out where the problem was and fixed it.
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Database connection

    Quote Originally Posted by poporacer View Post
    Every problem is covered in the manual or documented elsewhere so I could become an expert and respond to every post and say "Read the Manual"!!!
    It's good that you know where to look for answers - most people don't. The docs are equipped with search functionality so it is really easy to find things once you know what you are looking for. However posting in the newbie section doesn't mean you are free from doing any research yourself to at least get grasp of the basics before you start doing any actual programming.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Database connection

    I know there are people out there that won't do the research and just post their questions. I did research the problem...The manual stated "add this line to your qmake .pro file QT+=sql" I couldn't find a qmake.pro file but I did find a qmake in the projects and tried to add it there...it didn't work. I tried several variations to no avail. The manual was not real clear for a newbie on this issue. And thanks for your input. I am sure I will post more issues while I am learning and I will try to research the issue first....but if I can't find it or don't understand, don't beat me up for asking a dumb question.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Database connection

    It's 'qmake .pro', not 'qmake.pro' so the English meaning is different. And it's enough to read about qmake a bit to know that you have to run 'qmake' for it to see any changes in your .pro (not 'your.pro' ) file. I know this is not obvious and here we sometimes remind people that you have to run qmake afterwards but if you did that in every piece of the documentation it would be treating people like idiots.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Threads and database connection
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 7th August 2013, 09:30
  2. connection to sqlite database using qt4
    By elimelick in forum Newbie
    Replies: 0
    Last Post: 25th July 2010, 15:53
  3. Database connection problem
    By MarkoSan in forum Qt Programming
    Replies: 1
    Last Post: 11th September 2008, 21:18
  4. database connection
    By peace_comp in forum Qt Programming
    Replies: 4
    Last Post: 13th May 2008, 13:16
  5. Qt and MySQL Database Connection
    By shamik in forum Qt Programming
    Replies: 41
    Last Post: 6th October 2006, 13:48

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.