PDA

View Full Version : QSqlQuery



AlbertoN
15th December 2013, 15:21
Hi all,

i can't understand where's my mistake.



#include <QApplication>
#include <QtSql>
#include <QtWidgets/QMessageBox>

bool m_hastransaction = false;
bool m_startedtransaction = false;
QSqlDatabase db;


void connection()
{
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
m_startedtransaction = false;
m_hastransaction = false;
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::Ok);
return;
}
m_hastransaction = db.driver()->hasFeature(QSqlDriver::Transactions);
}

void startTransaction()
{
if(m_hastransaction)
m_startedtransaction = db.transaction();
}

bool endTransaction(bool value)
{
if(!m_hastransaction || !m_startedtransaction)
return value;
else{
if(value)
{
db.commit();
}else{
db.rollback();
}
m_startedtransaction = false;
}
return value;
}

bool UnloggedQueryExec(QString query,bool setForward)
{//it doesn't work
startTransaction();
QSqlQuery q(query);//QSqlQuery q = QSqlQuery(query);//it's the same
q.setForwardOnly(setForward);
bool ret = q.exec();
if(!ret){
qDebug() << "*** UnloggedQueryExec: query failed: " << query << q.lastError().text();
}else{
qDebug() << "*** UnloggedQueryExec: query ok";
}
return endTransaction(ret);
}

bool UnloggedQueryExec1(QString query,bool setForward)
{//it works
startTransaction();
QSqlQuery q;
q.setForwardOnly(setForward);
bool ret = q.exec(query);
if(!ret){
qDebug() << "*** UnloggedQueryExec1: query failed: " << query << q.lastError().text();
}else{
qDebug() << "*** UnloggedQueryExec1: query ok";
}
return endTransaction(ret);
}

int main(int argc, char *argv[])
{
QApplication a(argc,argv);

connection();
UnloggedQueryExec("CREATE TABLE log(id INTEGER PRIMARY KEY,descr varchar(30))",true);
UnloggedQueryExec1("CREATE TABLE log(id INTEGER PRIMARY KEY,descr varchar(30))",true);

return a.exec();
}




Output:
*** UnloggedQueryExec: query failed: "CREATE TABLE log(id INTEGER PRIMARY KEY,descr varchar(30))" "table tipolog already exists Unable to fetch row"
*** UnloggedQueryExec1: query ok


EDIT: ops i forget pro file, even if it should not matter:



QT += core widgets sql
CONFIG += console
TARGET = test
TEMPLATE = app

SOURCES += main.cpp

OBJECTS_DIR = build/o
MOC_DIR = build/moc
UI_DIR = build/ui
RCC_DIR = build/rcc
DESTDIR = bin



Someone could be so kind to help me?
Thanks in advance,
Alberto

ChrisW67
15th December 2013, 20:10
What is unclear about the error message your database engine is giving you?

Output:
*** UnloggedQueryExec: query failed: "CREATE TABLE log(id INTEGER PRIMARY KEY,descr varchar(30))"
"table tipolog already exists Unable to fetch row"


BTW: it helps if the code you post and the errors you present match

AlbertoN
15th December 2013, 22:30
Thanks for your replay.

Yes, furthermore It's unclear to me why it works if I create the query in this way:


QSqlQuery q;
q.exec(query);

while it doesn't work if I create it in these other way:


QSqlQuery q(query);
QSqlQuery q = QSqlQuery(query);


This is not so clear even because there is an example in qt docs like mine(http://qt-project.org/doc/qt-5.0/qtsql/qsqlquery.html#details):


QSqlQuery query("SELECT country FROM artist");
while (query.next()) {
QString country = query.value(0).toString();
doSomething(country);
}

Though the error message of database engine is not so clear as I never created log table before therefore It shouldn't happen that kind of error message.

ChrisW67
16th December 2013, 03:59
As described in the docs, QSqlQuery::QSqlQuery() with a string argument will create the query and execute it. Calling exec() a second time might have unintended consequences, like trying to create the same table again.

AlbertoN
16th December 2013, 10:19
Ops i miss it: ", it will be executed".

Thanks and sorry for noob question :p