#-- coding: utf-8 -- from PyQt6 import QtCore, QtGui, QtWidgets import pyodbc def…
- User
#-- coding: utf-8 -- from PyQt6 import QtCore, QtGui, QtWidgets import pyodbc
def connect(): conn_str = 'DRIVER={SQL Server};SERVER=SAVITAR\SQLEXPRESS01;DATABASE=kayit;Trusted_Connection=yes;' connection = pyodbc.connect(conn_str) return connection
class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(616, 779) font = QtGui.QFont() font.setBold(True) font.setItalic(False) font.setUnderline(False) font.setWeight(75) font.setStrikeOut(False) MainWindow.setFont(font) self.centralwidget = QtWidgets.QWidget(parent=MainWindow) self.centralwidget.setObjectName("centralwidget") self.lineEdit = QtWidgets.QLineEdit(parent=self.centralwidget) self.lineEdit.setGeometry(QtCore.QRect(160, 60, 251, 31)) self.lineEdit.setText("") self.lineEdit.setObjectName("lineEdit") self.pushButton = QtWidgets.QPushButton(parent=self.centralwidget) self.pushButton.setGeometry(QtCore.QRect(420, 160, 91, 81)) self.pushButton.setObjectName("pushButton") self.pushButton.clicked.connect(self.guncelle) self.label = QtWidgets.QLabel(parent=self.centralwidget) self.label.setGeometry(QtCore.QRect(0, 60, 121, 31)) font = QtGui.QFont() font.setFamily("Calibri") font.setPointSize(12) self.label.setFont(font) self.label.setAutoFillBackground(False) self.label.setTextFormat(QtCore.Qt.TextFormat.AutoText) self.label.setWordWrap(False) self.label.setObjectName("label") self.label_2 = QtWidgets.QLabel(parent=self.centralwidget) self.label_2.setGeometry(QtCore.QRect(0, 100, 121, 51)) font = QtGui.QFont() font.setFamily("Calibri") font.setPointSize(12) self.label_2.setFont(font) self.label_2.setObjectName("label_2") self.lineEdit_2 = QtWidgets.QLineEdit(parent=self.centralwidget) self.lineEdit_2.setGeometry(QtCore.QRect(160, 110, 251, 31)) self.lineEdit_2.setText("") self.lineEdit_2.setObjectName("lineEdit_2") self.label_3 = QtWidgets.QLabel(parent=self.centralwidget) self.label_3.setGeometry(QtCore.QRect(0, 150, 121, 51)) font = QtGui.QFont() font.setFamily("Calibri") font.setPointSize(12) self.label_3.setFont(font) self.label_3.setObjectName("label_3") self.lineEdit_3 = QtWidgets.QLineEdit(parent=self.centralwidget) self.lineEdit_3.setGeometry(QtCore.QRect(160, 160, 251, 31)) self.lineEdit_3.setText("") self.lineEdit_3.setObjectName("lineEdit_3") self.label_4 = QtWidgets.QLabel(parent=self.centralwidget) self.label_4.setGeometry(QtCore.QRect(0, 210, 151, 31)) font = QtGui.QFont() font.setFamily("Calibri") font.setPointSize(12) self.label_4.setFont(font) self.label_4.setAutoFillBackground(False) self.label_4.setTextFormat(QtCore.Qt.TextFormat.AutoText) self.label_4.setWordWrap(False) self.label_4.setObjectName("label_4") self.lineEdit_4 = QtWidgets.QLineEdit(parent=self.centralwidget) self.lineEdit_4.setGeometry(QtCore.QRect(160, 210, 251, 31)) self.lineEdit_4.setText("") self.lineEdit_4.setObjectName("lineEdit_4") self.pushButton_2 = QtWidgets.QPushButton(parent=self.centralwidget) self.pushButton_2.setGeometry(QtCore.QRect(420, 60, 91, 81)) self.pushButton_2.setObjectName("pushButton_2") self.pushButton_2.clicked.connect(self.ekle) self.pushButton_3 = QtWidgets.QPushButton(parent=self.centralwidget) self.pushButton_3.setGeometry(QtCore.QRect(520, 60, 91, 81)) self.pushButton_3.setObjectName("pushButton_3") self.pushButton_3.clicked.connect(self.sil) self.pushButton_4 = QtWidgets.QPushButton(parent=self.centralwidget) self.pushButton_4.setGeometry(QtCore.QRect(520, 160, 91, 81)) self.pushButton_4.setObjectName("pushButton_4") self.tableWidget = QtWidgets.QTableWidget(parent=self.centralwidget) self.tableWidget.setGeometry(QtCore.QRect(0, 270, 611, 481)) self.tableWidget.setObjectName("tableWidget") self.tableWidget.setColumnCount(0) self.tableWidget.setRowCount(0) MainWindow.setCentralWidget(self.centralwidget) self.statusbar = QtWidgets.QStatusBar(parent=MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) try: # Veritabanına bağlantı oluşturma connection = connect() # Bağlantı üzerinden bir cursor (imleç) oluşturma cursor = connection.cursor() # SQL sorgusuyla tablodan verileri alma cursor.execute("SELECT ad, id, sehir, sektor FROM Musteriler") data = cursor.fetchall() # Veritabanı bağlantısını ve cursoru kapatma cursor.close() connection.close() # Tabloya verileri ekleme self.tabloyu_guncelle() # Tablo başlıkları header_labels = ["Müşteri Adı", "Müşteri No", "Şehir", "Sektor"] self.tableWidget.setHorizontalHeaderLabels(header_labels) except pyodbc.Error as error: print("Hata:", error)
- Assistant
The given code is a PyQt6 application that connects to a SQL Server database and allows the user to add, update, and delete records in the "Musteriler" table.
The
connectfunction establishes a connection to the SQL Server database using the pyodbc library.The
Ui_MainWindowclass defines the user interface of the application. It has methods for setting up the UI, handling button clicks, updating the table, adding records, and deleting records.The
setupUimethod sets up the UI components such as labels, line edits, buttons, and the table widget. It also fetches the data from the "Musteriler" table using a SQL query and populates the table widget with the data.The
retranslateUimethod sets the text and labels of the UI components.The method updates the table widget with the current data from the "Musteriler" table.
- User
bana türkçe anlat. Ben uygulamayı çalıştırıp tabloda bir satırı seçip guncelle butonuna tıkladığımda yeni bir ekranda boş linelar gelsin istiyorum. Bu linelara veritabanında update edilecek değerleri yazıp okey dediğimde değerler yazdığım değerler ile güncellenecek
- Assistant
- User
satır güncellendi printi veriyor fakat tabloda gözükmüyor
- Assistant
Özür dilerim, hata nedeniyle güncellenen verilerin tabloda görünmemesi durumu oluşmuş. Sorunu düzeltmek için
verileri_guncellemetodunu şu şekilde güncelleyebilirsiniz:def verileri_guncelle(self, row, ad, no, sehir, sektor, dialog): try: connection = connect() with connection: cursor = connection.cursor() query = "UPDATE Musteriler SET ad = ?, id = ?, sehir = ?, sektor = ? WHERE id = ?" cursor.execute(query, (ad, no, sehir, sektor, no)) connection.commit() print(f"Satır güncellendi: {ad} - {no} - {sehir} - {sektor}") self.tabloyu_guncelle() dialog.close() except pyodbc.Error as error: print("Hata