Need Help with QT and SQLite
Hi,
In my application I want to use a SQLite Database which I have it in the File . I created the connection as shown in the examples and I want display only one value from the the data base in a label on the main window.
this is the code i have
Code:
qwery.exec("Select pwd from pwdtbl where Role = 1");
QString result
= qwery.
value(record.
indexOf("pwd")).
toString();
ui->lblMWinput->text()= result;
if this is not the right way can please someone direct me in the right way ..
Thanks,
Regards.
Re: Need Help with QT and SQLite
From the QSqlQuery::exec () docs:
Quote:
After the query is executed, the query is positioned on an invalid record and must be navigated to a valid record before data values can be retrieved (for example, using next()).
You should also check that the exec() call succeeded rather than discard its return value.
Re: Need Help with QT and SQLite
First of all thanks ChrisW67 for the Quick response
Coming to the Question now i am trying it this way ..
My code is
Connection. h
Code:
#ifndef CONNECTION_H
#define CONNECTION_H
#include <QMessageBox>
#include <QtSql/QSQLiteDriver>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include <QDebug>
static bool createConnection()
{
db.setDatabaseName("./RMD0_2.db");
db.open();
qDebug()<< db <<db.isOpen();
if (!db.open()) {
QMessageBox::critical(0, qApp
->tr
("Cannot open database"),
qApp->tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
return false;
}
return true;
}
#endif // CONNECTION_H
And the main.cpp
Code:
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <connection.h>
#include <QMessageBox>
int main(int argc, char *argv[])
{
if (!createConnection()){
return 1;
}
MainWindow w;
w.show();
return a.exec();
}
The mainwindow.cpp
Code:
void MainWindow::menuAction()
{
QString qexe
= "SELECT STAFF_ID FROM EVENT_STAFF WHERE EVENT_STAFF_ID = 2";
q.prepare(qexe);
if(!q.exec())
qDebug()<< q.lastError();
else
mm.show();
}
my out put is :
QSqlDatabase(driver=""QSQLITE"", database=""./RMD0_2.db"", host="""", port=-1, user="""", open=true) true
and when i click on button menuAction in the mainwindow.cpp
QSqlError(-1, "Unable to fetch row", "No query")
what can be the problem.?
Re: Need Help with QT and SQLite
Chetu, for a test, try changing your code like this:
Code:
q.exec( "SELECT STAFF_ID FROM EVENT_STAFF WHERE EVENT_STAFF_ID = 2");
q.last();
qDebug() << "query error is " << q.lastError(); // just to check
QString staff_id
= q.
value(0).
toString();
// get the value from select field 0 qDebug() << "staff_id is now " << staff_id; // check the value
I think you will find this works.
You only need to do a query.prepare then a query .exec if you have parameters you need to substitute like event_staff_id using query.addBindValue(<value>);
Re: Need Help with QT and SQLite
Thanks for the help waynew
when i tried to execute the code as you said the output is as follows
Quote:
Starting C:\Qt\2010.05\qt\TestRM-build-desktop\debug\TestRM.exe...
QSqlDatabase(driver=""QSQLITE"", database=""./RMD0_2.db"", host="""", port=-1, user="""", open=true) true
query error is QSqlError(1, "Unable to execute statement", "no such table: EVENT_STAFF")
QSqlQuery::value: not positioned on a valid record
staff_id is now ""
and the port = -1 what does that signify..
Re: Need Help with QT and SQLite
I am sorry .. the problem was with the database path ..
now everything works fine
Re: Need Help with QT and SQLite
Glad you got it working ok!