PDA

View Full Version : SQLITE database problems



phoenix
30th April 2007, 21:09
Hello all,

I am using QT4 and kdevelop3.4 to connect to a sqlite database. Following is my code, and i get some errors as i listed below.

Connection.h
#ifndef CONNECTION_H
#define CONNECTION_H

#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QVariant>

static bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("/home/LInux-ADVS5/advs -joe/RealBasic/IntialWizard/advsdb_1.rbd");
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"
"Click Cancel to exit."), QMessageBox::Cancel);
return false;
}
return true;
}

main.cpp
#include <QApplication>
#include "connection.h"
#include "main_application.h"

#include <QSqlQuery>
#include <QSqlRecord>





int main(int argc, char *argv[])
{
QString str,name,s;
QApplication app(argc, argv);
main_application * mw = new main_application();
if (!createConnection())
return 1;

QSqlQuery q("select * from AlarmAction");
QSqlRecord recordSet = q.record();
int rec = recordSet.count();

qDebug("count is = %d", rec);

int nameCol = recordSet.indexOf("ID"); // index of the field "name"
while (q.next())
{
qDebug("names are:%s", q.value(nameCol).toString());
}

mw->show();
return app.exec();
}


The warning msg i get is
main.cpp:52: warning: cannot pass objects of non-POD type ‘class QString’ through ‘...’; call will abort at runtime


am i overlooking something?
I am a newbie in QT4. Thanks in advance for your help..

--PH

marcel
30th April 2007, 21:26
As far I can see, your problem is right about here:


qDebug("names are:%s", q.value(nameCol).toString());


Formatting with %s requires const char*. QString does not have an operator const char*, so you must use:


qDebug("names are:%s", q.value(nameCol).toString().toAscii().constData()) ;


BTW: could you use code tags for source code. It is more readable that way.

Regards

phoenix
30th April 2007, 21:34
Thanks a lot for your help. It works now..
I shall use code tags for source code from now on.

Thanks again..

--PH

marcel
30th April 2007, 21:38
You're welcome.