PDA

View Full Version : Sorry If this is a repost but something...



nnidza
3rd January 2007, 15:04
I am using a QT4.1.4 with the PSQL8.1.
Sorry for reposting this problem but I am having problems with the setFilter
method. I saw a plenty of posts in the forum but I just can't make it
work?!
Here is what I have in the constructor:

filt::filt( QWidget * parent, Qt::WFlags f)
: QDialog(parent, f)
{
//I have created a form with two buttons OK, CANCEL and a
//table View named by default tableView. In the class I have
//defined a model as a QSqlTableModel.

setupUi(this);

model = new QSqlTableModel(this);
model->setTable("Krv");
model->setFilter("GrupaID = 1");
model->select();

tableView->setModel(model);
tableView->setVisible(true);


}

I have tried

model->setFilter("GrupaID = /"1/""); -< for strings
model->setFilter("Grupa = /"A+/"");
model->setFilter("Grupa = 'A+'");

I tried using a QString in the brackets, but...

When I build my project I get a form with no data?!
When I remove the setFilter line I got the data filled in the
tableView. I tried all sort of filtering (I used it on numbers, int4,
strings, with other tables but i am missing something).
I tried almost all posts in forums but as I said i am missing
something obviously...

Here is a table structure:

CREATE TABLE "Krv"
(
"GrupaID" int8 NOT NULL DEFAULT nextval('redni'::regclass),
"Grupa" varchar(3),
CONSTRAINT "Krv_pkey" PRIMARY KEY ("GrupaID")
)
WITHOUT OIDS;
ALTER TABLE "Krv" OWNER TO postgres;

And here is some table data:

"6";"A-"
"7";"A+"
"8";"B-"
"9";"B+"
"10";"AB+"
"11";"AB-"
"12";"O-"
"13";"O+"

wysota
3rd January 2007, 15:07
Does the same query executed manually (i.e. using QSqlQuery) works? If not, what is the error? You can obtain the error text using QSqlError.

nnidza
3rd January 2007, 15:40
OK, I want to say that I am an absolute novice to QT programming and my C++ is below average. So I'll give my best to explain what is going on, and I'll try to implement what ever you say.
After the
setupUI(this) i wrote the following code:

QSqlQuery query;
query.exec("SELECT * FROM Krv");
if (!query.isActive()){
QMessageBox::warning(this, tr("Database Error"),
query.lastError().text());
} else {
while (query.next()) {
int gid = query.value(0).toInt();
QString gr = query.value(1).toString();
cout << qPrintable(gr) << ": " << gid << endl;
}
}


And then I get the following error:

ERROR: Relation "krv" does not exist
QPSQL: Unable to create a query

Is this correct?

wysota
3rd January 2007, 16:50
No, it's not correct :) What if you change the table name from Krv to krv?

jacek
3rd January 2007, 17:56
query.exec("SELECT * FROM Krv");
It should be:
query.exec("SELECT * FROM \"Krv\"");
Once you started to use " around table and column names, you have to stick with it, because PostgreSQL converts all other identifiers to lowercase.

nnidza
3rd January 2007, 20:22
I've been using Microsoft SQL Server for years together with MS Access so I never thought this could be a problem:-(.
Sorry once again for reposting...
Thanks once again!