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[])
{
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();
}
query.prepare("SELECT * FROM USER_INFO");
query.exec();
qDebug() << "Rows affected = " << query.numRowsAffected() << endl;
db.close();
app.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
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
#-------------------------------------------------
#
# 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
To copy to clipboard, switch view to plain text mode
Bookmarks