from PyQt6 import QtCore, QtGui, QtWidgets import pyodbc def connect(): connstr =…
- User
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.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) conn_str = 'DRIVER={SQL Server};SERVER=SAVITAR\SQLEXPRESS01;DATABASE=kayit;Trusted_Connection=yes;' try: # Veritabanına bağlantı oluşturma connection = pyodbc.connect(conn_str) # 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.tableWidget.setRowCount(len(data)) self.tableWidget.setColumnCount(4 if len(data)==0 else len(data[0])) # Kolon sayısını verilere göre ayarla for row_num, row_data in enumerate(data): for col_num, col_data in enumerate(row_data): item = QtWidgets.QTableWidgetItem(str(col_data)) self.tableWidget.setItem(row_num, col_num, item) # 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) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.pushButton.setText(_translate("MainWindow", "GÜNCELLE")) self.label.setText(_translate("MainWindow", "MUSTERİ ADI:")) self.label_2.setText(_translate("MainWindow", "MÜŞTERİ NO:")) self.label_3.setText(_translate("MainWindow", "ŞEHİR:")) self.label_4.setText(_translate("MainWindow", "MÜŞTERİ SEKTÖR:")) self.pushButton_2.setText(_translate("MainWindow", "EKLE")) self.pushButton_3.setText(_translate("MainWindow", "SİL")) self.pushButton_4.setText(_translate("MainWindow", "GÖRÜNTÜLE")) def ekle(self): try: connection = connect() musteri_adi = self.lineEdit.text() musteri_no = self.lineEdit_2.text() sehir = self.lineEdit_3.text() musteri_sektor = self.lineEdit_4.text() # Veritabanına bağlantı oluşturma ve with bloğu içinde bağlantıyı kullanma with connection: # Bağlantı üzerinden bir cursor oluşturma cursor = connection.cursor() # Müşteri verilerini eklemek için INSERT INTO sorgusu query = "INSERT INTO Musteriler (ad, id, sehir, sektor) VALUES (?, ?, ?, ?)" cursor.execute(query, (musteri_adi, musteri_no, sehir, musteri_sektor)) # Değişiklikleri kaydetme ve işlemi tamamlama connection.commit() print("Müşteri başarıyla eklendi.") # Yeni müşteri ekledikten sonra tabloya verileri ekleme kısmı row_count = self.tableWidget.rowCount() self.tableWidget.insertRow(row_count) self.tableWidget.setItem(row_count, 0, QtWidgets.QTableWidgetItem(musteri_adi)) self.tableWidget.setItem(row_count, 1, QtWidgets.QTableWidgetItem(musteri_no)) self.tableWidget.setItem(row_count, 2, QtWidgets.QTableWidgetItem(sehir)) self.tableWidget.setItem(row_count, 3, QtWidgets.QTableWidgetItem(musteri_sektor)) # Bağlantıyı kapatma connection.close() except pyodbc.Error as error: print("Hata:", error) def sil(self): try: # Bağlantıyı al connection = connect()
- Assistant
This is a simple PyQt6 application that connects to a SQL Server database and allows the user to add, delete, and view customer information.
The
connectfunction establishes a connection to the SQL Server database using thepyodbcmodule.The
Ui_MainWindowclass is responsible for setting up the UI components and handling the logic of the application. ThesetupUimethod sets up the UI elements like labels, text fields, buttons, and the table widget. It also retrieves data from the database and populates the table widget with the data.The
retranslateUimethod sets the text and labels for the UI elements.The
eklemethod is triggered when the "EKLE" button is clicked. It retrieves the input values from the text fields, inserts the data into the database, and updates the table widget with the new data.The
silmethod is triggered when the "SİL" button is clicked. It retrieves the selected row from the table widget, retrieves the customer ID from the row and deletes the corresponding entry from the database. It also removes the selected row from the table widget.Finally, the block creates an instance of the PyQt6 application, sets up the main window, and starts the event loop.
- User
conn_str = 'DRIVER={SQL Server};SERVER=SAVITAR\SQLEXPRESS01;DATABASE=kayit;Trusted_Connection=yes;' try: # Veritabanına bağlantı oluşturma connection = pyodbc.connect(conn_str) # 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() bu kısmı connection fonksiyonunu çağırarak düzeltir misin
- Assistant
Certainly! You can modify the code to use the
connectfunction instead of directly creating a connection object:conn_str = 'DRIVER={SQL Server};SERVER=SAVITAR\SQLEXPRESS01;DATABASE=kayit;Trusted_Connection=yes;' 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() # ... Rest of the codeBy calling the
connectfunction, you can obtain a connection object that can be used for executing SQL queries and fetching data. - User
ekle ve silden sonra tablo güncelleme işleminide tek bir fonksiyon içerisinde oluşturup diğer fonksiyonlarda sadece bu fonksiyonu çağırarak tabloyu güncellemek istiyorum
- Assistant
- User
türkçe anlatır mısın
- Assistant
Elbette! Kodunuzu, tablo widget'ını en son verilerle güncellemek için ayrı bir fonksiyon içermesi şeklinde yeniden düzenleyebilirsiniz. Bu işlev, bir kayıt ekledikten veya sildikten sonra çağrılabilir.
Aşağıda, kodunuzu nasıl yeniden düzenleyebileceğinizi gösteren bir örnek bulunmaktadır:
def tabloyu_guncelle(self): try: connection = connect() # Tablo widget'ını temizle self.tableWidget.clearContents() self.tableWidget.setRowCount(0) # Veritabanından verileri getir with connection: cursor = connection.cursor() cursor.execute("SELECT ad, id, sehir, sektor FROM Musteriler") data = cursor.fetchall() # Tablo widget'ını yeni verilerle güncelle self.tableWidget.setRowCount(len(data)) self.tableWidget.setColumnCount(4 if len(data)==0 else len(data[0])) for row_num, row_data in enumerate(data): for col_num, col_data in enumerate(row_data): item = QtWidgets.QTableWidgetItem(str(col_data)) self.tableWidget.setItem(row_num, col_num, item) except pyodbc.Error as error: print("Hata:", error) def ekle(self): try: connection = connect() musteri_adi = self.lineEdit.text() musteri_no = self.lineEdit_2.text() sehir = self.lineEdit_3.text() musteri_sektor = self.lineEdit_4.text() with connection: cursor = connection.cursor() query = "INSERT INTO Musteriler (ad, id, sehir, sektor) VALUES (?, ?, ?, ?)" cursor.execute(query, (musteri_adi, musteri_no, sehir, musteri_sektor)) connection.commit() print("Müşteri başarıyla eklendi.") # tabloyu_guncelle fonksiyonunu çağırarak tablo widget'ını güncelleme self.tabloyu_guncelle() connection.close() except pyodbc.Error as error: print("Hata:", error) def sil(self): try: connection = connect() selected_items = self.tableWidget.selectedItems() if selected_items: selected_row = selected_items[0].row() musteri_no = self.tableWidget.item(selected_row, 1
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.