I am learning Python in order to write some programs for a friend who runs a small business. I have a lot of experience with programming but can’t seem to figure out how to get the data from the SQLite3 database to show up in the widgets I created (.ui) in Designer. Thanks in advance for any assistance you can provide!!
Here’s the coding for one of the widgets from the .ui file.
<widget class="QLineEdit" name="code">
<property name="geometry">
<rect>
<x>280</x>
<y>50</y>
<width>60</width>
<height>20</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>60</width>
<height>20</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>60</width>
<height>20</height>
</size>
</property>
<property name="text">
<string/>
</property>
<property name="maxLength">
<number>6</number>
</property>
<property name="alignment">
set>Qt::AlignmentFlag::AlignLeading|Qt::AlignmentFlag::AlignLeft|Qt::AlignmentFlag::AlignVCenter</set>
</property>
</widget>
Here’s my Python code with my feeble attempts removed. The program does execute and the screen created in Designer does appear.
import os
import sys
import sqlite3
import PySide6
from PySide6.QtUiTools import QUiLoader
from PySide6.QtCore import QSize
from PySide6.QtSql import QSqlDatabase, QSqlQuery, QSqlQueryModel
from PySide6.QtWidgets import (
QApplication,
QComboBox,
QDataWidgetMapper,
QDoubleSpinBox,
QFormLayout,
QLabel,
QLineEdit,
QMainWindow,
QSpinBox,
QWidget,
)
loader = QUiLoader()
basedir = os.path.dirname(__file__)
class MainUI: # Not a widget.
def __init__(self):
super().__init__()
self.ui = loader.load(
os.path.join(basedir, "customer.ui"), None
)
self.ui.setWindowTitle("Integrity Customer Maintenance")
self.db = QSqlDatabase("QSQLITE")
self.db.setDatabaseName(os.path.join(basedir, "integrity.db"))
self.db.open()
self.connection_obj = sqlite3.connect('integrity.db')
self.cursor_obj = self.connection_obj.cursor()
self.sys_cust_code = "mayhew"
#self.sys_cust_code = "333333"
self.cursor_obj.execute('SELECT * FROM customer WHERE cust_code = ?', (self.sys_cust_code,))
self.customer_rec = self.cursor_obj.fetchone() # holds data from customer database
self.code = self.customer_rec[0]
self.name = self.customer_rec[1]
print ("Key = ",self.sys_cust_code)
print(self.customer_rec)
print("")
print("Printing Individual Fields")
print("Code = ", self.customer_rec[0])
print("Name = ", self.customer_rec[1])
self.ui.code
self.mapper = QDataWidgetMapper() # One for all widgets
self.ui.show()
app = QApplication(sys.argv)
ui = MainUI()
app.exec()