PDA

View Full Version : Problem Opening Sqlite database from Qt



ronak.vashi
21st October 2012, 14:18
I am facing problem in openning sqlite database from Qt using QSqlDatabase. The db file and relevant tables are already created.
Single row is also inserted.

What can be the issue ? Tried configuring Qt Source with option "-qt-sql-sqlite" as given in help but no luck :(

Output after running application is:


Error!!!....openning db

LastError= ""
Rows affected = 0


Sample Code:




#include <QCoreApplication>
#include <QtSql>
#include <QDebug>

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("/home/data/db/sample.db");

if(!db.open())
{
qDebug() << "Database successfully opened...!!! go ahead !!!" << endl;
}
else
{
qDebug() << "Error!!!....openning db" << endl;
qDebug() << "LastError= " << db.lastError().databaseText();
}

QSqlQuery query;
query.prepare("SELECT * FROM USER_INFO");
query.exec();

qDebug() << "Rows affected = " << query.numRowsAffected() << endl;

db.close();
app.exec();
}


Project Pro file:




#-------------------------------------------------
#
# Project created by QtCreator 2012-10-14T19:20:29
#
#-------------------------------------------------

QT += core

QT -= gui
QT += sql

TARGET = DBTest
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp

unix:!macx:!symbian: LIBS += -L$$PWD/../../../../opt/QtSDK/QtCreator/lib/qtcreator/plugins/sqldrivers/ -lqsqlite

INCLUDEPATH += $$PWD/../../../../opt/QtSDK/Desktop/Qt/4.8.1/gcc/include/Qt
DEPENDPATH += $$PWD/../../../../opt/QtSDK/Desktop/Qt/4.8.1/gcc/include/Qt

Ashkan_s
21st October 2012, 17:18
correct your condition it must be
if(db.open())

Added after 59 minutes:

Also take look at QSqlQuery::numRowsAffectedyou will find
Note that for SELECT statements, the value is undefined; use size() instead.

ronak.vashi
29th October 2012, 12:18
This is has been solved. !!!