PDA

View Full Version : join is not working



godlesplay
24th January 2016, 15:57
hi could anyone help me? I want join two tables into one, this code is working on my friends qt+db2 but on my qt+ingres isn't.


void FouDialog::on_pushButton_4_clicked()
{
int num_rows, r, c;
QSqlQuery q(db);

if(!q.exec("SELECT count(id_wizyty) as num_rows FROM wizyta")) qDebug() << q.lastError().text();
q.first();
num_rows = q.value(0).toInt();

ui->tableWidget->setRowCount(num_rows);

//if(!q.exec("SELECT * FROM wizyta ")) qDebug() << q.lastError().text();
if(!q.exec("SELECT id_wizyty, data, w.id_pacjenta, id_lekarza, p.imie as Imie_Pacjenta ,p.nazwisko as Nazwisko_Pacjenta FROM wizyta w JOIN pacjent p ON w.id_pacjenta = p.id_pacjenta")) qDebug() << q.lastError().text();
for(r = 0, q.first(); q.isValid(); q.next(), ++r){
for(c = 0; c < 6; ++c){
ui->tableWidget->setItem(r,c, new QTableWidgetItem(q.value(c).toString()));
}
}
}

Lesiok
24th January 2016, 17:48
What do you mean "is not working" ?

godlesplay
24th January 2016, 17:52
columns from pacjent tables are not added into wizyta table

ChrisW67
25th January 2016, 20:10
columns from pacjent tables are not added into wizyta table
Nothing in your code tries to add columns (or rows) to any database table.


Does the query exec() call return true? Does your lastError() report anything?

Does
SELECT id_wizyty, data, w.id_pacjenta, id_lekarza, p.imie as Imie_Pacjenta ,p.nazwisko as Nazwisko_Pacjenta
FROM wizyta w
JOIN pacjent p ON w.id_pacjenta = p.id_pacjenta
Execute in isql and return what you expect?

Does the execution enter your for loop? Is there a reason you did not use the more idiomatic:


while (q.next()) {
// do stuff
}

godlesplay
25th January 2016, 20:45
Nothing in your code tries to add columns (or rows) to any database table.


Does the query exec() call return true? Does your lastError() report anything?

Does
SELECT id_wizyty, data, w.id_pacjenta, id_lekarza, p.imie as Imie_Pacjenta ,p.nazwisko as Nazwisko_Pacjenta
FROM wizyta w
JOIN pacjent p ON w.id_pacjenta = p.id_pacjenta
Execute in isql and return what you expect?

0 errors, in visual SQL it returns what it should

ChrisW67
26th January 2016, 05:09
Has your program successfully open()ed the database?
Does the code you have shown us actually get executed?
Does the query returning the count() work?

godlesplay
26th January 2016, 08:06
Database if opened successfully, I can add, edit, remove and see all records from my database in qt app, I just can't use join to see records from two tables in one.

godlesplay
26th January 2016, 10:51
ok I fixed it with ui->tableWidget->setColumnCount(6); and now it is working as it should, thanks for all your help, regards!