Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Thanks for the Awesome Front-End for Python3x! Currently running Python312x + MariaDB 11.4.2 + Linux-amd64 + sddm/lxqt & Need to know how to aggregate id column with auto_increment + primary_key, not_null instead of the built in one on "QTableWidgetItem"

Thanks for the Awesome Front-End for Python3x! Currently running Python312x + MariaDB 11.4.2 + Linux-amd64 + sddm/lxqt & Need to know how to aggregate id column with auto_increment + primary_key, not_null instead of the built in one on "QTableWidgetItem"

Scheduled Pinned Locked Moved Unsolved Qt for Python
1 Posts 1 Posters 199 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • digitalninjaD Offline
    digitalninjaD Offline
    digitalninja
    wrote on last edited by
    #1

    Thanks for the Awesome Front-End for Python3x! Currently running Python312x + MariaDB 11.4.2 + Linux-amd64 + sddm/lxqt & Need to know how to aggregate id column with auto_increment + primary_key, not_null instead of the built in one on "QTableWidgetItem"

    ython3.12x - Linux amd64 - sddm/lxqt - sublime text 4 demo editor - PySide6 (GUI Development/Front-End) --- Following a descent tutorial and learning; however I ran into a problem due to the fact that the tutorial does not have id & auto_increment w/ primary_key setup on their database like I do (I do on all my Databases MySQL/MariaDB) (PySide6 has a Versatile MySQL/MariaDB Connector; works for both out of the box so to speak).

    "QTableWidgetItem" is my problem ... It requires 3 Arguments per Traceback Errors & Documentation...

    https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QTableWidget.html#PySide6.QtWidgets.QTableWidget.setItem

    row
    
    column
    
    item – QTableWidgetItem
    

    I tried to manually strip it out... and found out about the 3 required. Below the following block: I will include the full code. Is there an alternative I could use other than "QTableWidgetItem" that allows me more functionality, efficiency so that I can properly utilize the ID column 0 that I setup on all my databases (Zero Compromise); This is purely a front-end. Back-end will always end up being full-text and requires ID auto_increment + primary_key + not_null

    Any Remedies... 100%.

    It doesn't launch flawless right now (load data was working right; however I just wanna go straight to the point and re-build now if required and it appears it needs one).

    Good Lookin!

    [CODE]
    def load_data(self):
    cursor = self.create_connection().cursor()

        sqlquery = "SELECT * FROM american_allegiant_list"
    
        # Set The Row Count As The Number of American Allegiants in The List
        self.table.setRowCount(len(sqlquery))
    
        # Add Each American Allegiants From The Database To The Table
        #table_row = 0
    
        # Get All Records
        cursor.execute(sqlquery)
        records = cursor.fetchall()
    
        for i in records:
            #self.table.setItem(QTableWidgetItem(i[0]))
            #self.table.setItem(QTableWidgetItem(i[1]))
            #self.table.setItem(QTableWidgetItem(i[2]))
            #self.table.setItem(QTableWidgetItem(i[3]))
            #self.table.setItem(QTableWidgetItem(i[4]))
            #self.table.setItem(QTableWidgetItem(i[5]))
    
            #self.table.setItem(table_row, 0, QTableWidgetItem(i[0]))
            self.table.setItem(table_row, 1, QTableWidgetItem(i[1]))
            self.table.setItem(table_row, 2, QTableWidgetItem(i[2]))
            self.table.setItem(table_row, 3, QTableWidgetItem(i[3]))
            self.table.setItem(table_row, 4, QTableWidgetItem(i[4]))
            self.table.setItem(table_row, 5, QTableWidgetItem(i[5]))
            #table_row = table_row + 1
    
        self.mydb.commit()
        self.mydb.close
    

    [/CODE]

    Full Code:

    [CODE]

    Date Started: 06/29/2024

    Time Started: AM

    Date Updated: 06/29/2024

    Time Updated: 03:02PM

    Date Finished:

    Time Finished:

    import sys
    import os
    from pathlib import Path

    from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QLabel, QDialog
    from PySide6.QtCore import Qt, QFile
    from PySide6.QtUiTools import QUiLoader
    from PySide6.QtGui import QIcon, QPixmap, QScreen

    import mysql.connector
    from PySide6.QtWidgets import QPushButton, QWidget, QHBoxLayout, QVBoxLayout, QMessageBox, QTableWidget, QTableWidgetItem, QLabel, QLineEdit, QGroupBox

    class Widget(QWidget):
    def init(self):
    super().init()
    self.setWindowTitle("PySide6 CRUD MariaDB | American Allegiant Edition [YTube Instructor: Fast Indian]")
    # Vertical / Horizon Set Geometry Values (Don't Quite Understand Yet)
    # Apparently Until "h_layout" is Vertical
    # Vertical: 100 x 100 ?
    # Horizontal: 500 x 100 ?
    self.setGeometry(100, 100, 500, 100)

        # Create QLabels
        id_label = QLabel("ID")
        name_label = QLabel("Name")
        unearnedincome_hobby_label = QLabel("Unearned Income Hobby")
        address_label = QLabel("Address [Creepy AF]")
        chronological_age_label = QLabel("Chronological Age")
    
        # Create QLine Edits
        self.id_line_edit = QLineEdit()
        self.name_line_edit = QLineEdit()
        self.unearnedincome_hobby_line_edit = QLineEdit()
        self.address_line_edit = QLineEdit()
        self.chronological_age_line_edit = QLineEdit()
    
        # Create QPushButtons for this form
        button_add_data = QPushButton("Add New Row")
        #button_add_data.clicked(self.add_data)
    
        button_update_data = QPushButton("Update Selected Row")
        #button_udpate_data.clicked(self.update_data)
    
    
        # The Starting of Horizontal PySide6 Layouts...
    
    
        # QLabel(s):
    
        # (Name) (0/4) : QLabel and QLine Edit Horizontal Layout
        h_layout0 = QHBoxLayout()
        h_layout0.addWidget(id_label)
        h_layout0.addWidget(self.id_line_edit)
    
        # (Name) (1/4) : QLabel and QLine Edit Horizontal Layout
        h_layout1 = QHBoxLayout()
        h_layout1.addWidget(name_label)
        h_layout1.addWidget(self.name_line_edit)
    
        # (Unearned Income Hobby) (2/4) : QLabel and QLine Edit Horizontal Layout
        h_layout2 = QHBoxLayout()
        h_layout2.addWidget(unearnedincome_hobby_label)
        h_layout2.addWidget(self.unearnedincome_hobby_line_edit)
        
        # (Address) (3/4) : QLabel and QLine Edit Horizontal Layout
        h_layout3 = QHBoxLayout()
        h_layout3.addWidget(address_label)
        h_layout3.addWidget(self.address_line_edit)
        
        # (Chronological Age) (4/4) : QLabel and QLine Edit Horizontal Layout
        h_layout4 = QHBoxLayout()
        h_layout4.addWidget(chronological_age_label)
        h_layout4.addWidget(self.chronological_age_line_edit)
    
    
        # QPush Buttons Horizontal Layout
        h_layout5 = QHBoxLayout()
        h_layout5.addWidget(button_add_data)
        h_layout5.addWidget(button_update_data)
    
    
        # Group Labels and Line Edit Items
        add_form = QGroupBox("Add New Unearned Income Free American Citizen!")
    
        # Layout Items in this Group Vertically
        form_layout = QVBoxLayout()
        form_layout.addLayout(h_layout0)
        form_layout.addLayout(h_layout1)
        form_layout.addLayout(h_layout2)
        form_layout.addLayout(h_layout3)
        form_layout.addLayout(h_layout4)
        form_layout.addLayout(h_layout5)
        add_form.setLayout(form_layout)
    
        # Create QTable
        self.table = QTableWidget(self)
        self.table.setMaximumWidth(850)
    
        self.table.setColumnCount(5)
        #self.table.setColumnWidth(0, 150)
        self.table.setColumnWidth(1, 150)
        self.table.setColumnWidth(2, 150)
        self.table.setColumnWidth(3, 150)
        self.table.setColumnWidth(4, 116)
        self.table.setColumnWidth(5, 150)
    
        self.table.setHorizontalHeaderLabels(["ID", "Name", "Unearned Income Hobby", "Address (Creepy AF)", "Chronological Age"])
    
        # Create More Push Buttons
        button_insert_data = QPushButton("Insert Test Data!")
        button_insert_data.clicked.connect(self.insert_data)
    
        button_load_data = QPushButton("Load Data!")
        button_load_data.clicked.connect(self.load_data)
    
        button_call_data = QPushButton("Extract Data!")
        #button_extract_data.clicked.connect(self.call_data)
    
        button_delete_data = QPushButton("Delete Row!")
        #button_delete_row.clicked.connect(self.delete_data)
    
    
        # Display All Items (Elements) within GroupBox
        layout = QVBoxLayout()
        layout.addWidget(add_form)
        layout.addWidget(self.table)
        layout.addWidget(button_insert_data)
        layout.addWidget(button_load_data)
        layout.addWidget(button_call_data)
        layout.addWidget(button_delete_data)
        self.setLayout(layout)
    
    def create_connection(self):
        # Create MariaDB Database Connection
        self.mydb = mysql.connector.connect(
            host="localhost",
            user="digitalninja",
            password="__x3eVjef0pEiG3UyK9bf__ovIXMdpgXG8zLqk6Z__",
            database="StupidThreat_YT_PySide6_CRUD_MySQL"
    
        )
        return self.mydb 
    
    def insert_data(self):
        # Create Cursor Object to Execute MariaDB Queries
    
        cursor = self.create_connection().cursor()
    
        # Create List of Tuples for Test Data...
        self.list_of_american_natural_born_citizen_allegiants = [
            ("Jasmine Thompson","Musician/Singer/Muse","USA Lawful Immigrant; Seattle, WA",18),
            ("Odey Rush (Israeli)","Actress/Muse","USA Lawful Immigrant; Hollywood, CA",18),
            ("Amanda Seyfried","Actress/Muse","Allentown, Pennsylvania",18),
            ("Cailee Spaeny","Actress/Muse","Springfield, Missouri",18),
    
        ]
    
        # Setup MessageBox For INSERT Test Data into Selected MariaDB 11.4.2 Database/Table!
        msgBox_INSERT_Test_Data_into_Selected_MariaDB_DB_TABLE = QMessageBox()
        
        # To Insert Multiple Rows
        msgBox_INSERT_Test_Data_into_Selected_MariaDB_DB_TABLE.setText("Now Attempting to INSERT Test Data into Selected MariaDB 11.4.2 Database/Table...")
        msgBox_INSERT_Test_Data_into_Selected_MariaDB_DB_TABLE.exec()
        cursor.executemany("insert into american_allegiant_list (name, hobby, address, chronological_age) values (%s, %s, %s, %s)", self.list_of_american_natural_born_citizen_allegiants)
        self.mydb.commit()
        # Add Exception Raising 
        msgBox_INSERT_Test_Data_into_Selected_MariaDB_DB_TABLE.setText("Attempting to INSERT Test Data into Selected MariaDB 11.4.2 Database/Table, Successful!")
        msgBox_INSERT_Test_Data_into_Selected_MariaDB_DB_TABLE.exec()
        #print("Attempting to INSERT Test Data into Selected MariaDB 11.4.2 Database/Table, Successful!")
        self.mydb.close 
    
    
    def load_data(self):
        cursor = self.create_connection().cursor()
    
        sqlquery = "SELECT * FROM american_allegiant_list"
    
        # Set The Row Count As The Number of American Allegiants in The List
        self.table.setRowCount(len(sqlquery))
    
        # Add Each American Allegiants From The Database To The Table
        #table_row = 0
    
        # Get All Records
        cursor.execute(sqlquery)
        records = cursor.fetchall()
    
        for i in records:
            #self.table.setItem(QTableWidgetItem(i[0]))
            #self.table.setItem(QTableWidgetItem(i[1]))
            #self.table.setItem(QTableWidgetItem(i[2]))
            #self.table.setItem(QTableWidgetItem(i[3]))
            #self.table.setItem(QTableWidgetItem(i[4]))
            #self.table.setItem(QTableWidgetItem(i[5]))
    
            #self.table.setItem(table_row, 0, QTableWidgetItem(i[0]))
            self.table.setItem(table_row, 1, QTableWidgetItem(i[1]))
            self.table.setItem(table_row, 2, QTableWidgetItem(i[2]))
            self.table.setItem(table_row, 3, QTableWidgetItem(i[3]))
            self.table.setItem(table_row, 4, QTableWidgetItem(i[4]))
            self.table.setItem(table_row, 5, QTableWidgetItem(i[5]))
            #table_row = table_row + 1
    
        self.mydb.commit()
        self.mydb.close
    
    
    def add_data(self):
        pass 
    
    def call_data(self):
        pass
    
    def update_data(self):
        pass
    
    def delete_data(self):
        pass
    

    [/CODE]

    @BRKastning (Twitter/X)

    https://github.com/BrandonKastning (My New Code and Major Projects)
    KastningBrandon (Everything I Fork and Random Ideas I started before getting my old account back).
    wethepeopleonline (USA Exodus/Mass Empowerments)

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved