1、
from ming import getconn
conn = getconn("gaoming")
print()
sql = [("select * from dept", ()),#"dept"的表中选择所有列
("delete from person where sid<%s", (4,)),#删除"person"表中"sid"列小于4的记录
("update person set sage=sage+%s", (5,))]#更新"person"表中"sage"列的值,将其增加5
with conn.cursor() as cs:
for cmd, param in sql:
ss = cmd.split()[0].lower()
eff = cs.execute(cmd, param)
if ss == 'select':
rows = cs.fetchall()
for row in rows:
print('-> ', row)
elif ss == 'update':
print("*** %d 行距离被更新 ***" % eff)
elif ss == 'delete':
print("*** %d 行距离被删除 ***" % eff)
elif ss == 'insert':
print("*** 成功插入%d 行记录 ***" % eff)
2、
import sys
import time
from datetime import datetime
from PyQt5.QtWidgets import *
# 创建一个应用程序
app = QApplication(sys.argv)
# 创建一个窗口
win = QWidget()
# 设置标题
win.setWindowTitle(" 一个简单的窗口和简单点击事件")
# 设置窗口的位置和大小
win.setGeometry(400, 400, 500, 350)
# 创建一个按钮
button1 = QPushButton("点击显示次数")
k = 1
# 定义按钮的事件处理
def on_button1_click():
global k
QMessageBox.information(win, "消息", "我被点击了" + str(k) + "次")
k += 1
# 创建一个垂直布局管理器,并将按钮和标签组件加 入其中
layout = QVBoxLayout()
layout.addWidget(button1)
# 将按钮的点击信号关联到事件处理函数
button1.clicked.connect(on_button1_click)
button2 = QPushButton("点击显示当前时间")
def on_button2_click():
while True:
print(datetime.now())
time.sleep(1)
layout.addWidget(button2)
button2.clicked.connect(on_button2_click)
# 设置布局为当前窗口的布局
win.setLayout(layout)
# 显示窗口
win.show()
# 运行应用程序
sys.exit(app.exec())
3、大作业
注册:
import sys
from PyQt5.QtWidgets import *
from ming import getconn
# 创建应用程序实例
app = QApplication(sys.argv)
# 创建窗口
win = QWidget()
win.setWindowTitle('注册界面')
#禁用窗口的大小更改
win.setFixedSize(500, 240)
# 创建表单布局
form_layout = QFormLayout()
# 创建标签和输入框
username_label = QLabel('用户名:')
username_input = QLineEdit()
form_layout.addRow(username_label, username_input)
password_label = QLabel('密码:')
password_input = QLineEdit()
password_input.setEchoMode(QLineEdit.Password) # 隐藏密码输入
form_layout.addRow(password_label, password_input)
confirm_password_label = QLabel('确认密码:')
confirm_password_input = QLineEdit()
confirm_password_input.setEchoMode(QLineEdit.Password) # 隐藏密码输入
form_layout.addRow(confirm_password_label, confirm_password_input)
# 创建注册按钮
register_button = QPushButton('注册')
register_button.clicked.connect(lambda: register(username_input.text(),
password_input.text(),
confirm_password_input.text()))
# 创建水平布局用于放置按钮
button_layout = QHBoxLayout()
button_layout.addStretch()
button_layout.addWidget(register_button)
button_layout.addStretch()
# 创建垂直布局用于放置表单和按钮
main_layout = QVBoxLayout()
main_layout.addLayout(form_layout)
main_layout.addLayout(button_layout)
# 设置窗口的布局
win.setLayout(main_layout)
# 定义注册逻辑
def register(username, password, confirm_password):
# 这里可以添加注册逻辑,例如检查密码是否匹配,然后保存用户信息等
if password == confirm_password:
print(f'用户名: {username}, 密码: {password}')
# 注册成功后的处理逻辑
conn = getconn("gaoming")
with conn.cursor() as cs:
eff = cs.execute("insert into user(username,password) "
"values(%s,%s)", (username, password))
conn.commit()
if eff:
print("*** 注册成功 ***")
# 注册成功弹出提示框
msg = QMessageBox(win)
msg.setWindowTitle("注册成功")
msg.setText("注册成功!")
msg.exec_()
win.close()
else:
print("*** 注册失败 ***")
else:
print('密码不匹配,请重新输入。')
# 密码不匹配的处理逻辑
# 显示窗口
win.show()
# 运行应用程序的事件循环
sys.exit(app.exec_())
登录:
import sys
from PyQt5.QtWidgets import *
from ming import getconn
# 创建应用程序实例
app = QApplication(sys.argv)
# 连接到数据库
conn = getconn("gaoming")
# 创建登录窗口
win = QWidget()
win.setWindowTitle('登录界面')
win.setGeometry(300, 300, 500, 240)
#禁用窗口的大小更改
win.setFixedSize(500, 240)
# 创建表单布局
form_layout = QFormLayout()
# 创建标签和输入框
username_label = QLabel('用户名:')
username_input = QLineEdit()
form_layout.addRow(username_label, username_input)
password_label = QLabel('密码:')
password_input = QLineEdit()
password_input.setEchoMode(QLineEdit.Password) # 隐藏密码输入
form_layout.addRow(password_label, password_input)
# 创建登录按钮
login_button = QPushButton('登录')
# 创建水平布局用于放置按钮
button_layout = QHBoxLayout()
button_layout.addStretch()
button_layout.addWidget(login_button)
button_layout.addStretch()
# 创建垂直布局用于放置表单和按钮
main_layout = QVBoxLayout()
main_layout.addLayout(form_layout)
main_layout.addLayout(button_layout)
# 设置窗口的布局
win.setLayout(main_layout)
# 登录函数
def login(username, password):
if not username or not password:
msg = QMessageBox(win) # 传递 win 作为父窗口
msg.setWindowTitle("登录失败")
msg.setText("用户名或密码不能为空,请重新输入。")
msg.exec_()
else:
with conn.cursor() as cs:
cs.execute("select * from user where username = %s and password = %s", (username, password))
result = cs.fetchone()
if result:
msg = QMessageBox(win) # 传递 win 作为父窗口
msg.setWindowTitle("登录成功")
msg.setText(f"用户名: {username}\n密码: {password}\n登录成功。")
msg.exec_()
else:
msg = QMessageBox(win) # 传递 win 作为父窗口
msg.setWindowTitle("登录失败")
msg.setText("用户名或密码错误,请重新输入。")
msg.exec_()
# 登录按钮绑定登录函数
login_button.clicked.connect(lambda: login(username_input.text(), password_input.text()))
# 显示窗口
win.show()
# 运行应用程序的事件循环
sys.exit(app.exec_())