Answer the question
In order to leave comments, you need to log in
How to correctly make changes and new data to the database table through gui on pyqt?
Just starting to do something in sqlite and PYQT and generally new to python and OOP.
The click_btn function, when clicked, should in theory add to the database table a new line of information that the user entered in lineEdit for each column.
click_btn prints out the values entered in lineEdit and applied to variables, but these values are not added to the database table and nothing happens to the database at all.
The database itself is working properly, the problem is entering values into it from the pyqt user interface. The code itself is clumsy, but I did not understand the essence of the problem itself
import sys
from PyQt6.uic import loadUi
from PyQt6 import QtWidgets
from PyQt6.QtSql import QSqlDatabase, QSqlTableModel
from PyQt6.QtWidgets import QDialog, QApplication, QMainWindow, QMessageBox, QTableView
from guijob import *
import sqlite3
class MainWindow(QDialog):
def __init__(self):
super(MainWindow, self).__init__()
loadUi("guijob.ui", self)
self.tableWidget.setColumnWidth(0, 150)
self.tableWidget.setColumnWidth(1, 150)
self.tableWidget.setColumnWidth(2, 150)
self.tableWidget.setColumnWidth(3, 150)
self.tableWidget.setColumnWidth(4, 150)
self.tableWidget.setColumnWidth(5, 150)
self.pushButton.clicked.connect(self.click_btn)
self.loaddata()
def loaddata(self):
with sqlite3.connect('Filmbase.db') as db:
cur = db.cursor()
cur.execute(""" CREATE TABLE IF NOT EXISTS Filmoteka(
name TEXT,
director TEXT,
surname_actor TEXT,
data TEXT,
rating TEXT,
views TEXT
) """)
sqlstr = 'SELECT * FROM Filmoteka LIMIT 100'
tablerow=0
results = cur.execute(sqlstr)
self.tableWidget.setRowCount(100)
for row in results:
self.tableWidget.setItem(tablerow, 0, QtWidgets.QTableWidgetItem(row[0]))
self.tableWidget.setItem(tablerow, 1, QtWidgets.QTableWidgetItem(row[1]))
self.tableWidget.setItem(tablerow, 2, QtWidgets.QTableWidgetItem(row[2]))
self.tableWidget.setItem(tablerow, 3, QtWidgets.QTableWidgetItem(row[3]))
self.tableWidget.setItem(tablerow, 4, QtWidgets.QTableWidgetItem(row[4]))
self.tableWidget.setItem(tablerow, 5, QtWidgets.QTableWidgetItem(row[5]))
tablerow+=1
db.commit()
def click_btn(self):
name = self.lineEdit.text()
director = self.lineEdit_2.text()
surname_actor = self.lineEdit_3.text()
data = self.lineEdit_4.text()
rating = self.lineEdit_5.text()
views = self.lineEdit_6.text()
print(name, director, surname_actor, data, rating, views)
insert = (name, director, surname_actor, data, rating, views)
with sqlite3.connect('Filmbase.db') as db:
cur = db.cursor()
query = """INSERT INTO Filmoteka (name, director, surname_actor, data, rating, views) VALUES (?, ?, ?, ?, ?, ?)"""
cur.execute(query, insert)
db.commit()
# main
app = QApplication(sys.argv)
mainwindow = MainWindow()
widget = QtWidgets.QStackedWidget()
widget.addWidget(mainwindow)
widget.setFixedHeight(850)
widget.setFixedWidth(1120)
widget.show()
try:
sys.exit(app.exec())
except:
print("Exiting")
Answer the question
In order to leave comments, you need to log in
https://newbedev.com/no-data-sources-are-configure... I
found a solution here, after configuring the data source to run sqlite
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question