PDA

View Full Version : Database connection



poporacer
5th September 2010, 02:22
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.


#include <QtGui>
#include <QtSql> //*** Error here
#include "dbmain.h"

bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("rider.mt");
if (!db.open()) {
QMessageBox::warning(0, QObject::tr("Database Error"),
db.lastError().text());
return false;
}
return true;
}

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
DBMain w;
w.show();

return a.exec();
}

Thanks for looking at this!

henzbelmont
5th September 2010, 03:38
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?

Talei
5th September 2010, 04:54
@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

poporacer
6th September 2010, 02:15
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"!!!

Talei
6th September 2010, 04:27
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.

wysota
6th September 2010, 08:53
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.

poporacer
6th September 2010, 23:48
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.

wysota
7th September 2010, 08:49
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.