MCPcopy Create free account
hub / github.com/Apress/modern-pyqt / createConnection

Method createConnection

ch04_database_handling/sql_manager.py:34–50  ·  view source on GitHub ↗

Set up connection to the database. Check if the tables needed exist.

(self)

Source from the content-addressed store, hash-verified

32 self.setupToolbar()
33
34 def createConnection(self):
35 """Set up connection to the database. Check if the tables needed exist."""
36 self.database = QSqlDatabase.addDatabase("QSQLITE")
37 self.database.setDatabaseName("databases/FishingStores.sql")
38
39 if not self.database.open():
40 print("Unable to open data source file.")
41 print("Connection failed: ", self.database.lastError().text())
42 sys.exit(1) # Error code 1 - signifies error in opening file
43
44 # Check if the tables we want to use exist in the database
45 tables_needed = {'customers', 'stores', 'orders', 'products', 'order_products'}
46 tables_not_found = tables_needed - set(self.database.tables())
47 if tables_not_found:
48 QMessageBox.critical(None, 'Error',
49 'The following tables are missing from the database: {}'.format(tables_not_found))
50 sys.exit(1) # Error code 1 – signifies error
51
52 def setupWindow(self):
53 """Set up the directory model/view instances, SQL model/view instances, and

Callers 1

initializeUIMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected