PyQt5: specifying QSqlTableModel

Rich Shepard rshepard at
Sat Jun 5 16:24:22 BST 2021

On Sat, 5 Jun 2021, Colin McPhail wrote:

> The Qt documentation for QSqlDatabase.setDatabaseName (qsqldatabase.html
> <>) says that
> the QSQLITE driver will create an empty database if it cannot find the
> specified one. Maybe the QPSQL driver does the same.


Both versions (mine and yours) find the database; the error was missing

> 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 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

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
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.


-------------- next part --------------
A non-text attachment was scrubbed...
Type: application/zip
Size: 2257 bytes
URL: <>

More information about the PyQt mailing list