PDA

View Full Version : QSqlQuery not working



BrainStorm
15th August 2010, 00:54
I'm stucked at this problem since a very long time ( about one day or more ), I just can't find what's wrong! it looks so simple and until now everything in the project did work fine, relational tables, and etc, well, I isolated it in a function, may I explain first, I've loaded a QComboBox with a QSqlQueryModel with this query: "SELECT name FROM Categories", ok. Then I need to insert the id of the selected item in another table (for using as relation), I made this function:


int ProductsForm::categoryIdByName(const QString& name)
{
QSqlQuery categories = QSqlQuery(model->database());
categories.prepare("SELECT id FROM Categories "
"WHERE name=:name)");
categories.bindValue(":name",name);
categories.exec();
if(categories.size()==-1)
{
qDebug() << "error";
return 1;
}
return cate

I call it with QComboBox::currentText(), BUT, as you see my concern there, query size aways return -1, no item! i've tried a lot of other querys, like "SELECT * FROM Categories", but nothing! I'm SURE the database, tables and items where created and inserted respectively, the program did work fine until this, I hope you guys can enlight-me and help me kick off this meaningless error. thanks in advance, sorry my bad english. :)

saa7_go
15th August 2010, 01:07
Try to identify the problem using QSqlQuery::lastError().

BrainStorm
15th August 2010, 01:45
Try to identify the problem using QSqlQuery::lastError().

it returns -1, no error =T

saa7_go
15th August 2010, 01:58
If you modify your codes to the following:


int ProductsForm::categoryIdByName(const QString& name)
{
QSqlQuery categories(model->database());
categories.exec(QString("SELECT id FROM Categories WHERE name='%1'").arg(name));
if(categories.size()==-1)
{
qDebug() << "error";
return 1;
}
.....
}


is categories.size() value still -1 ?

BrainStorm
15th August 2010, 02:22
If you modify your codes to the following:


int ProductsForm::categoryIdByName(const QString& name)
{
QSqlQuery categories(model->database());
categories.exec(QString("SELECT id FROM Categories WHERE name='%1'").arg(name));
if(categories.size()==-1)
{
qDebug() << "error";
return 1;
}
.....
}


is categories.size() value still -1 ?

yesss...:(

BrainStorm
15th August 2010, 02:33
int ProductsForm::categoryIdByName(const QString& name)
{
QSqlQuery categories = QSqlQuery(model->database());
categories.exec(QString("SELECT id FROM Categories WHERE name='%1'").arg(name));
categories.first();
return categories.value(0).toInt();

it works now, and i still don't know why! ^^" but thanks anyway :)

saa7_go
15th August 2010, 03:08
Are you using sqlite database? If you are using sqlite, it doesn't support reporting information about query sizes. So, this is why you always get -1.

BrainStorm
15th August 2010, 04:17
Are you using sqlite database? If you are using sqlite, it doesn't support reporting information about query sizes. So, this is why you always get -1.

yes, i'm using sqlite :eek:
:o

Lykurg
15th August 2010, 08:15
yes, i'm using sqlite
then you can check via
QSqlQuery categories = QSqlQuery(model->database());
categories.prepare("SELECT id FROM Categories "
"WHERE name=:name)");
categories.bindValue(":name",name);
categories.exec();
if (!categories.next())
{
// error and return;
}

categories.value(0)/*...*/

Lesiok
15th August 2010, 10:19
Is this code "copy and paste" ? If YES, You have an error in line 2. Signe ) after :name.

BrainStorm
16th August 2010, 00:35
then you can check via
QSqlQuery categories = QSqlQuery(model->database());
categories.prepare("SELECT id FROM Categories "
"WHERE name=:name)");
categories.bindValue(":name",name);
categories.exec();
if (!categories.next())
{
// error and return;
}

categories.value(0)/*...*/


i see.. but i think using QSqlQuery::next(); once is the same as using QSqlQuery::first(); :)

BrainStorm
16th August 2010, 00:42
By the way, does anyone knows how to create a button (flat would be nice) that drops a drop menu? :p

Lykurg
16th August 2010, 07:10
i see.. but i think using QSqlQuery::next(); once is the same as using QSqlQuery::first(); :)
Yes, missed that, since in the code snippet the return was not checked if it is valid.


By the way, does anyone knows how to create a button (flat would be nice) that drops a drop menu? :pUse a normal buttun and set a menu via QPushButton::setMenu(). for flat use QPushButton::setFlat().

BrainStorm
16th August 2010, 14:02
Use a normal buttun and set a menu via QPushButton::setMenu(). for flat use QPushButton::setFlat().

Ohh.. i see ^^ thanks, i was using QMenu::popup() with QPushButton::pos() :p