Moving Data from SQLite to .ui form created in Designer

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

Hi @WaltMayhew,
You need to set the text to the QLineEdit, but note that you can only have one line of text using this widget. So to display both nameand code values, you can either format the string (as in my example below) or use two dedicated widgets in your layout.

self.ui.code.setText(f'{self.name}: {self.code}')

I don’t know what the QDataWidgetMapper is supposed to do here. But this is more intended to be used with a model

Note that there are read-to-use models already provided by Qt, more info here: Qt - Using the SQL Model Classes that you may use in combination with a QDataWidgetMapper for mapping widgets on columns of your database.

1 Like

Thank you!!! You are a God-send!!!

I was able to populate all the TextEdits individually by using a variation of your code. I just copied the text and changed the name. It worked great!! Below is an example. I also took the value directly from the database input instead of populating an interim field. Below is an example.

self.ui.last_order.setText(f’{self.customer_rec[16]}:')

Glad that helped. You can also take a look to SQL examples (C++) from Qt. There are good examples of displaying and editing data using models and views.