PDA

View Full Version : Creating new QSqlDatabase



ToddAtWSU
6th February 2011, 16:49
I am trying to connect to a local SQLite database I have for a school project. We are free to use whatever language we want and I use Qt all the time at work, so I decided to use this over Java for me application. I thought by following the documentation, this wouldn't be too difficult but I am having problems. I am running Qt Creator 2.0.1 on Windows 7 with Qt 4.7.0. The following line in my CPP file causes an error:


QSqlDatabase database = QSqlDatabase::addDatabase( "QSQLITE" );

The compilation error I get is:


:: error: collect2: ld returned 1 exit status

Commenting this line out lets the code compile successfully. The whole function it is in is this:


bool MainWindow::connectToDatabase( )
{
QSqlDatabase database = QSqlDatabase::addDatabase( "QSQLITE" );
return true;
}

Obviously this function will have more in it later, and I will use a class variable instead of the local object, but if I can't get this simple statement to compile, no sense trying more complex things, yet.

Any ideas why my code wouldn't compile with this simple line? It is straight out of the documentation with the SQLite driver which from what I understand would have been created by default when I installed Qt Creator.

Thanks for your help!

Lykurg
6th February 2011, 16:59
Most likely you forget QT += sql in the pro file.

ToddAtWSU
6th February 2011, 17:57
You are correct. I have not really used .pro files. I originally did everything by hand and with makefiles in UNIX and recently I have used Qt Integrator with Netbeans for Solaris, but they have check mark boxes to select which Qt libraries you need so I just check the SQL library in the properties and don't realize what it does in the background. Thanks for your help!