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;
void connection()
{
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"
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();
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[])
{
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();
}
#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();
}
To copy to clipboard, switch view to plain text mode
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
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
To copy to clipboard, switch view to plain text mode
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
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
To copy to clipboard, switch view to plain text mode
Someone could be so kind to help me?
Thanks in advance,
Alberto
Bookmarks