PyQt5: specifying QSqlTableModel
Rich Shepard
rshepard at appl-ecosys.com
Sat Jun 5 16:24:22 BST 2021
On Sat, 5 Jun 2021, Colin McPhail wrote:
> The Qt documentation for QSqlDatabase.setDatabaseName (qsqldatabase.html
> <https://doc.qt.io/qt-5.15/qsqldatabase.html#setDatabaseName>) says that
> the QSQLITE driver will create an empty database if it cannot find the
> specified one. Maybe the QPSQL driver does the same.
Colin,
Both versions (mine and yours) find the database; the error was missing
tables.
> Are you certain that you are specifying the correct path to the database,
Yes. There were two errors in my version that I corrected. Running diff -y
shows no differences in the two files.
> The version of datasource.py that you are running has the correction I
> made to the indentation of the block of code that checks for the presence
> of the required tables. You should get the "repair the database" message
> box if an empty database is being used.
I fixed the indentation. The 'repair the database' message told me that QSql
does not want the PK sequence tables included in the table list. Fixed that.
No more message.
> To be certain of using the required database file you could pass a
> complete path ("/usr/rich/.../foods.db") to setDatabaseName().
It's not the database, but it was the two PK sequence tables that threw the
error.
Now: your food database displays the requested table in a QTableView. My
database does not display the activitytypes in a QTableView; only a blank
window. 'diff -y' shows no differences other than the names of the databases
and the tables.
Attached is a ZIPped file with the two .py files and the two .log files. The
log file says it cannot find the table 'activitytypes' but it is in the
database:
bustrac=# \d
List of relations
Schema | Name | Type | Owner
--------+---------------------------+----------+----------
public | activities | table | rshepard
public | activitytypes | table | rshepard
public | industrytypes | table | rshepard
public | locations | table | rshepard
public | organizations | table | rshepard
public | organizations_org_nbr_seq | sequence | rshepard
public | people | table | rshepard
public | people_person_nbr_seq | sequence | rshepard
public | projects | table | rshepard
public | statustypes | table | rshepard
I am not seeing why that table is not seen.
Regards,
Rich
-------------- next part --------------
A non-text attachment was scrubbed...
Name: activitytype-files.zip
Type: application/zip
Size: 2257 bytes
Desc:
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210605/ae69e50f/attachment.zip>
More information about the PyQt
mailing list