Как передавать данные с combobox python на сервер mysql&
Помогите решить проблему, а именно передача выбранных данных с combobox PyQt5 в mysql. Начинающий пользователь Python, потому не судите строго. Необходимо при выборе строки в ячейке передавать данные при нажатии кнопки в базу данных.
#!/usr/bin/python3
from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QAction, QMainWindow, QDialog, QLineEdit, QPushButton, QMessageBox, QVBoxLayout, QComboBox, QHBoxLayout, QWidget
from PyQt5.QtCore import pyqtSlot
import sys
import mysql.connector
from mysql.connector import Error
from mysql.connector import errorcode
##################################################
from PyQt5.QtGui import QIcon
####################################################
class App(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'Тест'
self.left = 200
self.top = 200
self.width = 300
self.height = 140
self.initUI()
def initUI(self):
# self.setWindowTitle(self.title)
# self.setGeometry(self.left, self.top, self.width, self.height)
# self.label = QLabel('Name',self)
# self.label.move(0,30)
# self.label.resize(41,19)
# self.label = QLabel('Email',self)
# self.label.move(0,90)
# self.label.resize(51,19)
# self.label = QLabel('Id',self)
# self.label.move(10,140)
# self.label.resize(31,19)
# self.textbox = QLineEdit(self)
# self.textbox.move(60, 20)
# self.textbox.resize(256, 41)
# self.textbox2 = QLineEdit(self)
# self.textbox2.move(60, 80)
# self.textbox2.resize(256, 41)
# self.textbox3 = QLineEdit(self)
# self.textbox3.move(60, 130)
# self.textbox3.resize(101, 31)
self.button = QPushButton('Добавить', self)
self.button.move(10, 40)
#############################
hbox = QHBoxLayout()
combo = QComboBox(self)
combo.addItem('Disk')
combo.setItemIcon(0, QIcon('disk.png'))
combo.addItem('Web')
combo.setItemIcon(1, QIcon('web.png'))
combo.addItem('Computer')
combo.setItemIcon(2, QIcon('computer.png'))
###############################################################################################
hbox.addWidget(combo) # tut doblaem!!!!!!!!!!!!!!!!
hbox.setSpacing(20)
self.setContentsMargins(20, 20, 20, 20)
self.setLayout(hbox)
################################################################################################
self.setGeometry(100, 100, 100, 50)
self.setWindowTitle('QComboBox')
self.show()
# self.button.clicked.connect(self.on_click)
self.show()
@pyqtSlot()
def on_click(self):
# name = self.textbox.text()
# email = self.textbox2.text()
# self.textbox.setText("")
# self.textbox2.setText("")
# id = self.textbox3.text()
# id = int(id)
#######################################
combo.activated[str].connect(self.onActivated)
######################################
#####################
self.hbox.setText(text)
self.hbox.adjustSize()
######################
try:
connection = mysql.connector.connect(host='localhost',
database='pyqt5test',
user='hookah12',
password='12345678')
sql = "INSERT INTO test (id,name, email) VALUES (%s,%s, %s)"
val = (id,name,email)
cursor = connection.cursor()
cursor.execute(sql,val)
connection.commit()
print(cursor.rowcount, "Record inserted successfully into Laptop table")
cursor.close()
except mysql.connector.Error as error:
print("Failed to insert record into Laptop table {}".format(error))
finally:
if (connection.is_connected()):
connection.close()
print("MySQL connection is closed")
# def InsertData(self):
# pass
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
ex.resize(120,84)
sys.exit(app.exec_())