connect mysql.

This commit is contained in:
rainerosion 2023-09-10 00:15:03 +08:00
parent a4d7a2c44a
commit 902aaae6c0
2 changed files with 110 additions and 6 deletions

114
dm.py
View File

@ -1,18 +1,33 @@
import socket
import sys
import pymysql
from PySide6 import QtWidgets
from PySide6.QtWidgets import QApplication, QMainWindow, QTableWidget, QTableWidgetItem, QVBoxLayout, QWidget, \
QTextEdit
QTextEdit, QPushButton, QHBoxLayout, QLineEdit, QMessageBox
class TableWidgetExample(QMainWindow):
def __init__(self):
super().__init__()
self.connection = None
self.cursor = None
self.is_connect = False
# self.button_add = None
# self.button_execute = None
# self.input_text = None
# self.connection_host = None
# self.connection_port = None
# self.connection_user = None
# self.connection_pwd = None
self.initUI()
def initUI(self):
self.setWindowTitle("弹幕采集")
self.setWindowTitle("采集")
self.setGeometry(100, 100, 800, 600)
central_widget = QWidget()
@ -61,14 +76,103 @@ class TableWidgetExample(QMainWindow):
bottom_widget = QWidget()
bottom_widget.setFixedHeight(200)
bottom_layout = QVBoxLayout(bottom_widget)
input_text = QTextEdit()
bottom_layout.addWidget(input_text)
self.input_text = QTextEdit()
self.input_text.setPlaceholderText("请输入链接,一行一个")
bottom_layout.addWidget(self.input_text)
connection_layout = QHBoxLayout()
self.connection_host = QLineEdit()
self.connection_host.setPlaceholderText("地址")
self.connection_port = QLineEdit()
self.connection_port.setPlaceholderText("端口")
self.connection_dbname = QLineEdit()
self.connection_dbname.setPlaceholderText("数据库")
self.connection_user = QLineEdit()
self.connection_user.setPlaceholderText("用户名")
self.connection_pwd = QLineEdit()
self.connection_pwd.setPlaceholderText("密码")
self.connection_pwd.setEchoMode(QLineEdit.Password)
self.button_connection = QPushButton("连接")
self.button_connection.clicked.connect(self.connect_to_database)
connection_layout.addWidget(self.connection_host)
connection_layout.addWidget(self.connection_port)
connection_layout.addWidget(self.connection_user)
connection_layout.addWidget(self.connection_pwd)
connection_layout.addWidget(self.button_connection)
bottom_layout.addLayout(connection_layout)
action_layout = QHBoxLayout()
button_add = QPushButton("添加链接")
action_layout.addWidget(button_add)
button_execute = QPushButton("开始采集")
action_layout.addWidget(button_execute)
bottom_layout.addLayout(action_layout)
center_layout.addWidget(bottom_widget)
# 按钮部分
def connect_to_database(self):
# 数据库基本信息
host = self.connection_host.text()
port = self.connection_port.text()
user = self.connection_user.text()
pwd = self.connection_pwd.text()
dbname = self.connection_dbname.text()
if not host or not port or not user or not pwd:
self.statusBar().showMessage("主机、端口、用户名和密码不能为空!")
QMessageBox.critical(self, "Error", "主机、端口、用户名和密码不能为空!")
return
try:
socket.gethostbyname(host)
except socket.error:
self.statusBar().showMessage("无效的主机!")
QMessageBox.critical(self, "Error", "无效的主机!")
return
else:
# 主机有效
print("有效的主机")
# 校验端口
try:
port = int(port)
if 0 < port <= 65535:
print("有效的端口")
else:
QMessageBox.critical(self, "Error", "端口超出范围!")
self.statusBar().showMessage("端口超出范围!")
return
except ValueError:
QMessageBox.critical(self, "Error", "无效的端口!")
self.statusBar().showMessage("无效的端口!")
return
try:
# 连接数据库
self.connection = pymysql.connect(host=host, port=port, user=user, password=pwd, db=dbname)
self.cursor = self.connection.cursor()
# 链接成功
self.is_connect = True
self.statusBar().showMessage("Connection succees!")
except pymysql.Error as e:
# 异常 连接失败
self.is_connect = False
QMessageBox.critical(self, "Error", str(e))
self.statusBar().showMessage("Connection failed")
def closeEvent(self, event):
if self.connection:
self.connection.close()
event.accept()
if __name__ == "__main__":
app = QApplication(sys.argv)

View File

@ -116,7 +116,7 @@ class SQLClient(QMainWindow):
splitter_handle.addWidget(bottom_widget)
central_widget.addWidget(splitter_handle)
self.db_type_input = QComboBox()
db_type_input = QComboBox()
self.db_type_input.addItems(["sqlite", "mysql", "pgsql"])
self.db_type_input.setPlaceholderText("Database Type")
connection_layout.addWidget(self.db_type_input)