Python: QSqlTableModel works on Windows but not on macOS
-
@JonB looking only for a solution :P . As you said it is very weird. I am sure that my code (and the ODBC driver) communicates with my MySQL server, otherwhise it should not be able to retrieve columns name.
As you suggest, I tried these lines of code:
query = QtSql.QSqlQuery("SELECT * FROM canzoni", db=db) print("Executed?:\t" + str(query.isActive())) print("Number of rows:\t" + str(query.size())) print("Number of columns:\t" + str(query.record().count())) '''Iterate over rows''' while query.next(): print(query.value(0)) '''Iterate over columns''' for index in range(0, query.record().count()): print(f"Name of columns {index}:\t" + query.record().fieldName(index))
And the result is only columns name. No rows (data) were retrieved.
Surfing the net, I found an old and similar topic on this forum (Click Me!) marked as solved (Spoiler: the solution was to update the Qt5... I did it before open the topic).
I really have no idea.
-
@DrTiaZ said in Python: QSqlTableModel works on Windows but not on macOS:
I really have no idea.
Nor do I, so these are just things I personally would try in an attempt to see where the problem might lie:
SELECT COUNT(*) FROM canzoni
SELECT 1, 2, 3
Do you get results back from either of these (exactly as written, as
QSqlQuery
s, notQSqlQueryModel
)? -
Hi,
Maybe a silly question and also to rule out the obvious: are hitting the same server (and database) from both Windows and macOS ?
-
@SGaist said in Python: QSqlTableModel works on Windows but not on macOS:
are hitting the same server from both Windows and macOS ?
OP has said:
(pointing the same server, the same database and the same table, using the same account)
I do hope we can take him at his word, else this is a wild goose chase...!
@DrTiaZ Please stake your life on this, we have had countless times people say they are using the same and then end up saying "Oh they got it wrong it's a different server" and that gets very irritating!! -
Certainly, you are right.
This is the MySQL server that I am pointing to:
It is reacheable using Terminal from macOS. There are no other databases call "test". As you can see, it contains 3 tables. Specifically, "canzoni" has 3 fields (canzoni_id, titolo, album_id).
I'm going to show you the best proof I can provide to ensure that I am operating on the correct server.
As you can see, the table "canzoni" contains the following 7 rows:
Now, if I try to execute an INSERT INTO statement via my script, using these lines
query_to_execute = "INSERT INTO canzoni (titolo, album_id) VALUES ('Test1', '1')" query = QtSql.QSqlQuery(query_to_execute, db=db)
the application correctly insert the new data into "canzoni".
So I can suppose that ODBC driver works correctly, my application is able to point the correct server and also to query it.
The problem using macOS is only to retrieve data from it.
If I try
SELECT COUNT(*) FROM canzoni;
the result is
-
@DrTiaZ
That implies to me that it is succeeding in retrieving the result set (rows) from the query from the database --- as opposed to never retrieving data. Don't know where that leaves you for your case though. Nor why Number of rows keeps being reported as -1. -
But also SELECT 1, 2, 3 fails to retrieve the queryset, in facts it retrieve only columns name and 0 row(s), like the queries before.
Documentation relative to QtSql.QSqlQuery.size() says "Returns the size of the result (number of rows returned), or -1 if the size cannot be determined or if the database does not support reporting information about query sizes. Note that for non-SELECT statements ( isSelect() returns false ), will return -1. If the query is not active ( isActive() returns false ), -1 is returned."
I really don't understand why this happens.
-
@DrTiaZ said in Python: QSqlTableModel works on Windows but not on macOS:
I really don't understand why this happens.
I would blame the ODBC driver then.
-
@DrTiaZ said in Python: QSqlTableModel works on Windows but not on macOS:
But also SELECT 1, 2, 3 fails to retrieve the queryset, in facts it retrieve only columns name and 0 row(s), like the queries before.
Ah, yes. I was expecting "No column name" as the titles for each column and thought the row shown was the single data row, but I think you're right.
I don't know where you go next, or what help you will get here. Does MacOS supply an ODBC tool to allow you to query/view the data?
-
@Christian-Ehrlicher said in Python: QSqlTableModel works on Windows but not on macOS:
@DrTiaZ said in Python: QSqlTableModel works on Windows but not on macOS:
I really don't understand why this happens.
I would blame the ODBC driver then.
I thought the same, but as @JonB suggested before, using iODBC tool and connecting to MySQL server via ODBC 8.0 Unicode Driver (same driver used in my Py script) I can view the data.
-
I'm not aware of any such problems in the ODBC driver since ages so I doubt it's a Qt problem.
-
@DrTiaZ
I'm with @Christian-Ehrlicher on this, though we can't say 100%. If you are able, you might try PyQt5 or 6, or even C++ if you can, to rule out PySide. Otherwise reinstall Qt and/or the ODBC driver/Python interface to that. Or even try a different version of Qt. And search to verify no known MacOS Qt/Py/ODBC issues. Or even a different MacOS machine if available! Other than that I'm not sure how much more help you can get here.