PDA

View Full Version : QSQLITE database changes not showing up in database



Cyrebo
14th April 2013, 16:23
I have two databases im working with. They are both for different form interfaces. One works but the other database doesn't show any changes when I make an update and I get no errors in my code so it's quite difficult to troubleshoot. Here is a listing of my db setup and where I update in both:

The working db:


#include <QtSql>

#define Path_to_DB "/media/.../dbpath"

.....

{
ui->setupUi(this);
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(Path_to_DB);
QFileInfo checkFile(Path_to_DB);

if(checkFile.isFile())
{
if(db.open())
{
qDebug() << "Connected to database file";
}
}else{
qDebug() << "Database file not found";
}
}

adminDialog::~adminDialog()
{
delete ui;
qDebug() << "Closing connection to Database file on exit...";
db.close();
}

double count;

void adminDialog::on_pushButton_3_clicked()
{
count++;
QString str = ui->lineEdit->text();
ui->count->display(count);

qDebug() << str;

if(!db.isOpen()){
qDebug() << "No connection to db";
return;
}

QSqlQuery query;

QString qry = QString("INSERT INTO customer (name, size1, size2, size3) VALUES ('%1', NULL, NULL, NULL)").arg(str);

query.prepare(qry);
if(query.exec())
{
qDebug() << "Update Complete";
}
else
{
qDebug() << query.lastError();
}
}

db thats not working:



#include <QtSql>

#define Path_to_DB "/media/.../db2path"
......

ui->setupUi(this);
db2 = QSqlDatabase::addDatabase("QSQLITE");
db2.setDatabaseName(Path_to_DB);
QFileInfo checkFile(Path_to_DB);

if(checkFile.isFile())
{
if(db2.open())
{
qDebug() << "Connected to database file";
}
}else{
qDebug() << "Database file not found";
}
}

void demo::on_pushButton_clicked()
{
if(!db2.isOpen()){
qDebug() << "No connection to db";
return;
}
........

QSqlQuery query;
switch (md_pref)
{
case 1:
query.prepare("UPDATE customer SET size1 = size1 + 1 WHERE name = 'Gary' ");
if(query.exec())
{
qDebug() << "Updated Gary";
}
else
{
qDebug() << query.lastError();
}
break;
.............

}


I'm working on a Linux Kubuntu 12.10 system. I appreciate all help on this thanks.

wysota
14th April 2013, 17:39
What does QSqlQuery::exec() return? What does prepare() return?

Cyrebo
14th April 2013, 17:49
What does QSqlQuery::exec() return? What does prepare() return?

Well I get no errors when running just like in the working version. The qDebug outputs updated but nothing actually happens.

wysota
14th April 2013, 19:55
And what does QSqlQuery::numRowsAffected() return?

Cyrebo
14th April 2013, 22:09
And what does QSqlQuery::numRowsAffected() return?

Returns the correct number of rows affected... :confused:

After doing some manual debugging i've seen the following in application output when I change between the windows that connect to the different databases.


QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.

Looks like its using the same connection for the two dbs not sure what to actually change though because when I tried changing something it gave me an error saying " Driver not loaded"....

wysota
14th April 2013, 22:34
If you want help then provide something we can compile and test. Otherwise you're on your own.

Cyrebo
14th April 2013, 23:18
It's working now! I hadn't set the default db value that was being updated to 0 like I did in the one that's working lol...smh

P.S. Found that out while I was preparing to upload all my code.