PDA

View Full Version : select all is now working



godlesplay
12th January 2016, 21:38
Welcome! I'm making project. GUI with database. There is MainWidnow with buttons which are opening new dialog window (for example secdialog). In these dialog windows I have tables, textfield and buttons to add,edit and remove records from my db. Everything is working nice. I have only one problem with SELECT *, I added "refresh database" button and after click it select * function should fill my table with existing records but it only fill table with column names. Could anyone help me? Thanks!

mainwindow.cpp -> http://wklej.to/Tby0s
secdialog.cpp -> http://wklej.to/ouFXd

If you need any other files, just write.

anda_skoa
13th January 2016, 10:54
There is no SELECT * in the code you posted.
Are you referring to the select in "on_pushButton_4_clicked"?

Have you tried passing the query string directly to QSqlQueryModel::query()?

Btw:
- you are leaking the query object in that method
- you will be leaking the model if you ever click the button again
- setModal(true) is unnecessary when the dialog is used with exec()

And I would highly recommend not to use the "connect by name" approach.

Cheers,
_

godlesplay
13th January 2016, 15:05
@anda_skoa I changed in ingres odbc driver settings from select loops to cursor and now it is working(with this code: https://www.youtube.com/watch?v=MMJfpbLbZRY) thanks!

godlesplay
1st February 2016, 21:40
So I'm here again... Select with tableWidget is working, but with tableView not, so I can't fetch data to make for example report. And I need it to pass my project on uni. Could anyone help me with this select?

anda_skoa
2nd February 2016, 08:00
Select as in SQL Select or in selecting items in the view?

Cheers,
_

godlesplay
2nd February 2016, 08:06
as SQL SELECT, I can see only column names

anda_skoa
2nd February 2016, 08:09
So if you use the same model in a QTableView and a QTableWidgets side-by-side, only one is showing data?

Cheers,
_

godlesplay
2nd February 2016, 08:10
here is example of class with select using tablewidget which is working http://wklej.to/iQ3lb and here with tableview which isn't http://wklej.to/VL2tt

anda_skoa
2nd February 2016, 09:29
One difference is that in the second case you are not passing a DB connection, while you do so in the first one.

Is the debug output for the first value of the first record what you expect?
Is the model's rowCount() what it should be?

Cheers,
_

P.S.: you most likely don't want to create a new model everytime the button is clicked

godlesplay
2nd February 2016, 11:38
What do you mean by :you are not passing a DB connection" ? TableViews doesn't have rowCount() method.

anda_skoa
2nd February 2016, 12:29
What do you mean by :you are not passing a DB connection" ?

In the code that you claim to be working you are passing a "db" object to the QSqlQuery's constructor, but you don't pass that object to the QSqlQueryModel constructor.



TableViews doesn't have rowCount() method.
Did someone say otherwise?

Cheers,
_

godlesplay
2nd February 2016, 12:52
so what should I do? can you tell me step by step? my english isn't perfect so sorry for that

anda_skoa
2nd February 2016, 14:49
If you have "db" set up as a special database connection, use it as a constructor argument for the model.

Then check that the exact same query you are trying to pass to the model works using QSqlQuery.

Then check the model after you have given it the query:
- does it have rows (check rowCount())?
- does it have data in these rows?

Cheers,
_

godlesplay
2nd February 2016, 14:59
here it is my class with connect to db http://wklej.to/Scoi1 could you help me us it as constructor argument for the model? I'm new in programming so I don't understand a lot of things. 90% of my code I "found" on tutorial on youtube

anda_skoa
2nd February 2016, 16:17
That looks ok.
I was just wondering because some of your code that you claimed to be working was explicitly using some "db" object while the code you said was not working was not.

Cheers,
_

godlesplay
2nd February 2016, 16:20
So do you have any idea how can I "fix" select? Because I need to make at least one report from my application to pass my project and without "working" select I can't do it.

anda_skoa
2nd February 2016, 16:38
First you need to discover where it goes wrong. I suggest you read more than just the first line of replies.

Cheers,
_

godlesplay
2nd February 2016, 16:45
Then check that the exact same query you are trying to pass to the model works using QSqlQuery.

Then check the model after you have given it the query:
- does it have rows (check rowCount())?
- does it have data in these rows?
I tried this http://wklej.to/GgQ2j and in console I have this error: "QSqlError("", "", "")" It still have column names only

anda_skoa
2nd February 2016, 17:58
I suggested reading the replies, not quoting.

Where in this code do you check the QSqlQuery?
Where in this code to you check the number of rows in the model?
Where to you compare the values of the rows with the values from the query?

Cheers,
_

godlesplay
2nd February 2016, 18:06
how check the QSqlQuery? When I check number of rows in the model using "qDebug() << model->rowCount();" it returns 0 in console. how to compate values of the rows with the values from the query?

anda_skoa
2nd February 2016, 19:44
how check the QSqlQuery?

Earlier you posted code that iterates through a query result, no?



When I check number of rows in the model using "qDebug() << model->rowCount();" it returns 0 in console.

Then it will be interesting to see if the query works directly.
Because it looks like there is no data for that query.



how to compate values of the rows with the values from the query?
Well, since your model says it has 0 rows there is no data to compare.

Cheers,
_

godlesplay
2nd February 2016, 19:58
So I have no idea why tableWidget can obtain my data from my database and tableView not.

anda_skoa
2nd February 2016, 21:31
The table widget does not obtain data, you are iterating over a query object there.
And the code you posted for the table widget case was a different then the one you tried with the query model.

Cheers,
_

godlesplay
2nd February 2016, 21:43
So I found another method to obtain data from db http://wklej.to/4MWsN but it returns values from 1st row, how can I obtain other data?

anda_skoa
2nd February 2016, 22:37
You could read the documentation of QSqlQuery, it even comes with an example.

Cheers,
_