Python+MySQL 登录注册小练习
本文最后更新于164天前,其中的信息可能已经过时,如有错误请在评论区悄悄告诉我~~~

一、MySQL部分

数据库创建表名为info的表,创表的SQL语句为

CREATE TABLE `info` (
  `id` int(11) PRIMARY KEY AUTO_INCREMENT,
  `username` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `phone` varchar(255) NOT NULL
) DEFAULT CHARSET=utf8;

二、Python部分

两个程序文件要在同一目录下

1.主程序

import tkinter as tk
from tkinter import messagebox
import re
from mysql import *

# 验证正则
num = re.compile(r'[0-9]')
Special_cha = re.compile(r'[^0-9a-zA-Z]')
phone_num = re.compile(r'(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}')


# 用户名验证函数
def username_fun(username_f):
    if num.match(username_f) is None and Special_cha.search(username_f) is None and 5 <= len(username_f) <= 12:
        return False
    else:
        return True


# 密码验证函数
def passwd_fun(passwd_f):
    if Special_cha.search(passwd_f) is None and 6 <= len(passwd_f) <= 15:
        return False
    else:
        return True


# 第二次密码验证函数
def password_fun(password_f, passwd_f):
    if password_f == passwd_f:
        return False
    else:
        return True


# 手机号验证函数
def phone_number_fun(phone_number_f):
    if phone_num.search(phone_number_f) is not None:
        return False
    else:
        return True


# 注册函数
def register_fun():
    username = register_username_b.get()
    passwd = register_pass_b.get()
    password = register_password_b.get()
    phone_number = register_phone_b.get()
    if username_fun(username):
        messagebox.showerror("注册失败", "用户名格式错误,请重试。")
        return False
    if passwd_fun(passwd):
        messagebox.showerror("注册失败", "密码格式错误,请重试。")
        return False
    if password_fun(passwd, password):
        messagebox.showerror("注册失败", "两次密码不一致,请重试。")
        return False
    if phone_number_fun(phone_number):
        messagebox.showerror("注册失败", "手机号格式错误,请重试。")
        return False
    dict_register = {"username": username, "password": passwd, "phone": phone_number}
    arg = find(username, phone_number)
    if arg[0] is not None:
        messagebox.showerror("注册失败", "用户名已存在,请重试。")
        return False
    elif arg[1] is not None:
        sel = tk.messagebox.askyesno(title='注册失败', message='电话号码已存在,是否尝试登录?')
        if sel:
            func(3)
            reset_ui()
            return None
        else:
            return False
    elif arg[0] is None and arg[1] is None:
        add(dict_register)
        messagebox.showinfo("注册成功", "欢迎," + username + "!请登录。")
        func(3)
        reset_ui()
        return None


# 登录函数
def login_fun():
    username = login_username_b.get()
    passwd = login_pass_b.get()
    if username_fun(username):
        messagebox.showerror("登录失败", "用户名格式错误,请重试。")
        return False
    arg = find(username, "?")
    if arg[0] is None:
        messagebox.showerror("登录失败", "用户不存在,请重试。")
        return None
    dictionary = list(arg[0])
    if arg[0] is not None and dictionary[2] != passwd:
        messagebox.showerror("登录失败", "密码错误,请重试。")
        return None
    elif arg[0] is not None and dictionary[2] == passwd:
        messagebox.showinfo("登录成功", "欢迎," + username + "!")
        login_data(dictionary, 0)
        reset_ui()
        return None


# 登录传入数据函数
def login_data(info, serial):
    global overall_situation
    if serial == 0:
        overall_situation = info
        menu_tite.set("欢迎," + info[1] + "使用本系统~~~")
        tite_phone(info[3])
        func(4)
        return None
    elif serial == 1:
        overall_situation = []
        func(1)
        return None


# 更改密码函数
def change_password():
    passwd = pass_button.get()
    new_passwd = pass_b.get()
    new_password = password_b.get()
    if passwd != overall_situation[2]:
        messagebox.showerror("更改失败", "原密码错误,请重试。")
        return False
    if overall_situation[2] == new_password:
        messagebox.showerror("更改失败", "新密码和原密码相同,请重试。")
        return False
    if passwd_fun(new_passwd):
        messagebox.showerror("更改失败", "新密码不符合要求,请重试。")
        return False
    if password_fun(new_passwd, new_password):
        messagebox.showerror("更改失败", "两次密码不一致,请重试。")
        return False
    new_data = {'password': new_passwd, 'id': overall_situation[0]}
    alter(new_data, 1)
    messagebox.showinfo("更改成功", "密码更改成功,请重新登录。")
    login_data(1, 1)
    reset_ui()
    return True


# 更改用户名函数
def change_user():
    global overall_situation
    new_user = user_button.get()
    if username_fun(new_user):
        messagebox.showerror("更改失败", "用户名格式错误,请重试。")
        return False
    if new_user == overall_situation[1]:
        messagebox.showerror("更改失败", "用户名未更改。")
        return False
    age = find(new_user, '?')
    if age[0] is not None:
        messagebox.showerror("更改失败", "用户名已存在,请重试。")
        return False
    new_data = {'username': new_user, 'id': overall_situation[0]}
    alter(new_data, 0)
    messagebox.showinfo("更改成功", "用户名更改成功。")
    overall = find(new_user, '?')
    overall_situation = overall[0]
    menu_tite.set("欢迎," + new_user + "使用本系统~~~")
    func(4)
    reset_ui()
    return None


# 注销账号函数
def sign_out():
    global overall_situation
    sel = tk.messagebox.askyesno(title='系统提示', message='确认注销账号?')
    if sel:
        delete(overall_situation[0])
        overall_situation = []
        messagebox.showinfo("系统提示", "注销成功!")
        func(1)
        return None
    return None


# 更改电话号码
def change_phone():
    global overall_situation
    new_phone = phone_new.get()
    if phone_number_fun(new_phone):
        messagebox.showerror("更改失败", "电话号码格式错误,请重试。")
        return False
    if overall_situation[3] == new_phone:
        messagebox.showerror("更改失败", "新密码和原密码相同,请重试。")
        return False
    age = find('?', new_phone)
    if age[1] is not None:
        messagebox.showerror("更改失败", "电话号码已存在,请重试。")
        return False
    new_data = {'phone': new_phone, 'id': overall_situation[0]}
    alter(new_data, 2)
    overall_situation[3] = new_phone
    messagebox.showinfo("更改成功", "电话号码更改成功。")
    func(4)
    tite_phone(new_phone)
    reset_ui()
    return None


# 更改电话标题显示
def tite_phone(info):
    new_info = info[:3] + '****' + info[7:]
    print(new_info)
    phone_tite.set(new_info)


def reset_ui():
    register_username_b.delete(0, tk.END)
    register_pass_b.delete(0, tk.END)
    register_password_b.delete(0, tk.END)
    register_phone_b.delete(0, tk.END)
    login_username_b.delete(0, tk.END)
    login_pass_b.delete(0, tk.END)
    pass_button.delete(0, tk.END)
    pass_b.delete(0, tk.END)
    password_b.delete(0, tk.END)
    user_button.delete(0, tk.END)
    phone_new.delete(0, tk.END)


# 关闭程序
def close_fun():
    sel = tk.messagebox.askyesno(title='系统提示', message='是否要退出系统?')
    if sel:
        window.quit()
        window.destroy()
    else:
        return None


# 创建主窗口
window = tk.Tk()
window.title("登录/注册小程序")
window.geometry("710x540")
# noinspection PyTypeChecker
window.resizable(0, 0)

# 创建Frame容器
frame_home = tk.Frame(window, width=710, height=540)
frame_register = tk.Frame(window, width=710, height=540)
frame_login = tk.Frame(window, width=710, height=540)
frame_menu = tk.Frame(window, width=710, height=540)
frame_pass = tk.Frame(window, width=710, height=540)
frame_user = tk.Frame(window, width=710, height=540)
frame_phone = tk.Frame(window, width=710, height=540)
# frame_phone.pack()

# 切换
frame_home.place(width=710, height=540, x=0)
frame_register.place(width=710, height=540, x=710)
frame_login.place(width=710, height=540, x=710)
frame_menu.place(width=710, height=540, x=710)
frame_pass.place(width=710, height=540, x=710)
frame_user.place(width=710, height=540, x=710)
frame_phone.place(width=710, height=540, x=710)
overall_situation = {}
menu_tite = tk.StringVar()
phone_tite = tk.StringVar()


def func(n):
    frames = [frame_home, frame_register, frame_login, frame_menu, frame_pass, frame_user, frame_phone]
    frames[n - 1].place(width=710, height=540, x=0)
    frames.pop(n - 1)
    for i in frames:
        i.place(width=710, height=540, x=710)


# <--首页开始-->
# 首页-标题
tk.Label(frame_home, text="欢迎使用本系统~~~", font=("华文仿宋", 18)).place(x=1, y=10, width=250, height=30)
# 首页-登录按钮
button_login = tk.Button(frame_home, text="登录", font=("华文仿宋", 18), command=lambda: func(3))
button_login.place(x=20, y=100, width=670, height=100)
# 首页-注册按钮
button_register = tk.Button(frame_home, text="注册", font=("华文仿宋", 18), command=lambda: func(2))
button_register.place(x=20, y=220, width=670, height=100)
# 首页-退出程序按钮
button_quit = tk.Button(frame_home, text="退出程序", font=("华文仿宋", 18), command=close_fun)
button_quit.place(x=20, y=340, width=670, height=100)
# <--首页结束-->

# <--注册开始-->
#  注册-标题
tk.Label(frame_register, text="注册", font=("华文仿宋", 30)).place(x=0, y=50, width=710, height=35)
# 注册-返回按钮
register_button = tk.Button(frame_register, text="< 返回", font=("华文仿宋", 16), command=lambda: func(1))
register_button.place(x=30, y=20, width=80, height=40)
# 注册-用户名
tk.Label(frame_register, text="用户名:", font=("华文仿宋", 14)).place(x=50, y=150, width=100, height=30)
register_username_b = tk.Entry(frame_register, font=("华文仿宋", 14))
register_username_b.place(x=160, y=150, width=250, height=30)
tk.Label(frame_register, text="字母数字组合且不能以数字开头,长度为5-12位", font=("等线", 10)).place(x=410, y=150, width=300, height=30)
# 注册-密码
tk.Label(frame_register, text="密码:", font=("华文仿宋", 14)).place(x=59, y=200, width=100, height=30)
register_pass_b = tk.Entry(frame_register, font=("华文仿宋", 14), show="*")
register_pass_b.place(x=160, y=200, width=250, height=30)
tk.Label(frame_register, text="字母数字组合,且长度为6-15位", font=("等线", 10)).place(x=410, y=200, width=210, height=30)
tk.Label(frame_register, text="确认密码:", font=("华文仿宋", 14)).place(x=40, y=250, width=100, height=30)
register_password_b = tk.Entry(frame_register, font=("华文仿宋", 14), show="*")
register_password_b.place(x=160, y=250, width=250, height=30)
# 注册-手机号
tk.Label(frame_register, text="手机号:", font=("华文仿宋", 14)).place(x=50, y=300, width=100, height=30)
register_phone_b = tk.Entry(frame_register, font=("华文仿宋", 14))
register_phone_b.place(x=160, y=300, width=250, height=30)
# 注册-提交按钮
register_submit_b = tk.Button(frame_register, text="注册", font=("华文仿宋", 15), command=register_fun)
register_submit_b.place(x=190, y=380, width=120, height=40)
# 注册-重置按钮
register_reset_b = tk.Button(frame_register, text="重置", font=("华文仿宋", 15), command=reset_ui)
register_reset_b.place(x=320, y=380, width=120, height=40)
# <--注册结束-->

#  <--登录开始-->
# 登录-标题
tk.Label(frame_login, text="登录", font=("华文仿宋", 30)).place(x=0, y=50, width=710, height=35)
# 登录-返回按钮
register_button = tk.Button(frame_login, text="< 返回", font=("华文仿宋", 16), command=lambda: func(1))
register_button.place(x=30, y=20, width=80, height=40)
# 登录-用户名
tk.Label(frame_login, text="用户名:", font=("华文仿宋", 14)).place(x=150, y=150, width=100, height=30)
login_username_b = tk.Entry(frame_login, font=("华文仿宋", 14))
login_username_b.place(x=260, y=150, width=250, height=30)
# 登录-密码
tk.Label(frame_login, text="密码:", font=("华文仿宋", 14)).place(x=159, y=200, width=100, height=30)
login_pass_b = tk.Entry(frame_login, font=("华文仿宋", 14), show="*")
login_pass_b.place(x=260, y=200, width=250, height=30)
# 登录-提交按钮
login_submit_b = tk.Button(frame_login, text="登录", font=("华文仿宋", 15), command=login_fun)
login_submit_b.place(x=260, y=300, width=120, height=40)
# 登录-重置按钮
login_reset_b = tk.Button(frame_login, text="重置", font=("华文仿宋", 15), command=reset_ui)
login_reset_b.place(x=390, y=300, width=120, height=40)
# <--登录结束-->

# <--登录菜单-->
#  标题
menu_label = tk.Label(frame_menu, textvariable=menu_tite, font=("华文仿宋", 18))
menu_label.place(x=10, y=10, height=30)
# 选项
menu_button_alter = tk.Button(frame_menu, text="更改用户名", font=("华文仿宋", 18), command=lambda: func(6))
menu_button_alter.place(x=20, y=60, width=670, height=86)
menu_button_phone = tk.Button(frame_menu, text="更改电话号码", font=("华文仿宋", 18), command=lambda: func(7))
menu_button_phone.place(x=20, y=156, width=670, height=86)
menu_button_user = tk.Button(frame_menu, text="更改密码", font=("华文仿宋", 18), command=lambda: func(5))
menu_button_user.place(x=20, y=252, width=670, height=86)
menu_button_quit = tk.Button(frame_menu, text="退出登录", font=("华文仿宋", 18), command=lambda: login_data(1, 1))
menu_button_quit.place(x=20, y=348, width=670, height=86)
menu_button_out = tk.Button(frame_menu, text="注销账号", font=("华文仿宋", 18), command=sign_out)
menu_button_out.place(x=20, y=444, width=670, height=86)
# <--登录菜单结束-->

# <--更改密码开始-->
# 标题
tk.Label(frame_pass, text="更改密码", font=("华文仿宋", 30)).place(x=0, y=50, width=710, height=35)
# 返回按钮
pass_quit = tk.Button(frame_pass, text="< 返回", font=("华文仿宋", 16), command=lambda: func(4))
pass_quit.place(x=30, y=20, width=80, height=40)
# 原密码
tk.Label(frame_pass, text="原密码:", font=("华文仿宋", 14)).place(x=50, y=150, width=100, height=30)
pass_button = tk.Entry(frame_pass, font=("华文仿宋", 14), show="*")
pass_button.place(x=160, y=150, width=250, height=30)
# 新密码
tk.Label(frame_pass, text="密码:", font=("华文仿宋", 14)).place(x=59, y=200, width=100, height=30)
pass_b = tk.Entry(frame_pass, font=("华文仿宋", 14), show="*")
pass_b.place(x=160, y=200, width=250, height=30)
tk.Label(frame_pass, text="字母数字组合,且长度为6-15位", font=("等线", 10)).place(x=410, y=200, width=210, height=30)
tk.Label(frame_pass, text="确认密码:", font=("华文仿宋", 14)).place(x=40, y=250, width=100, height=30)
password_b = tk.Entry(frame_pass, font=("华文仿宋", 14), show="*")
password_b.place(x=160, y=250, width=250, height=30)
# 提交按钮
pass_submit_b = tk.Button(frame_pass, text="更改", font=("华文仿宋", 15), command=change_password)
pass_submit_b.place(x=190, y=300, width=120, height=40)
# 重置按钮
pass_reset_b = tk.Button(frame_pass, text="重置", font=("华文仿宋", 15), command=reset_ui)
pass_reset_b.place(x=320, y=300, width=120, height=40)
# <--更改密码结束-->

# <--修改用户名开始-->
# 标题
tk.Label(frame_user, text="更改用户名", font=("华文仿宋", 30)).place(x=0, y=50, width=710, height=35)
# 返回按钮
user_quit = tk.Button(frame_user, text="< 返回", font=("华文仿宋", 16), command=lambda: func(4))
user_quit.place(x=30, y=20, width=80, height=40)
# 新用户名
tk.Label(frame_user, text="新用户名:", font=("华文仿宋", 14)).place(x=50, y=150, width=100, height=30)
user_button = tk.Entry(frame_user, font=("华文仿宋", 14))
user_button.place(x=160, y=150, width=250, height=30)
tk.Label(frame_user, text="字母数字组合且不能以数字开头,长度为5-12位", font=("等线", 10)).place(x=410, y=150, width=300, height=30)
# 提交按钮
user_submit_b = tk.Button(frame_user, text="更改", font=("华文仿宋", 15), command=change_user)
user_submit_b.place(x=190, y=300, width=120, height=40)
# 重置按钮
user_reset_b = tk.Button(frame_user, text="重置", font=("华文仿宋", 15), command=reset_ui)
user_reset_b.place(x=400, y=300, width=120, height=40)
# <--修改用户名结束-->

# <--修改密码开始-->
# 标题
tk.Label(frame_phone, text="更改电话号码", font=("华文仿宋", 30)).place(x=0, y=50, width=710, height=35)
# 返回按钮
phone_quit = tk.Button(frame_phone, text="< 返回", font=("华文仿宋", 16), command=lambda: func(4))
phone_quit.place(x=30, y=20, width=80, height=40)
# 提示
tk.Label(frame_phone, text="原电话号码为:", font=("华文仿宋", 15)).place(x=100, y=140, width=130, height=30)
phone_tips = tk.Label(frame_phone, textvariable=phone_tite, font=("华文仿宋", 16))
phone_tips.place(x=230, y=140, width=210, height=30)
# 新电话
tk.Label(frame_phone, text="新电话号码为:", font=("华文仿宋", 15)).place(x=100, y=210, width=130, height=30)
phone_new = tk.Entry(frame_phone, font=("华文仿宋", 15))
phone_new.place(x=250, y=210, width=250, height=30)
# 提交按钮
phone_submit_b = tk.Button(frame_phone, text="更改", font=("华文仿宋", 15), command=change_phone)
phone_submit_b.place(x=230, y=310, width=120, height=40)
# 重置按钮
phone_reset_b = tk.Button(frame_phone, text="重置", font=("华文仿宋", 15), command=reset_ui)
phone_reset_b.place(x=400, y=310, width=120, height=40)
# <--修改密码结束-->
window.mainloop()

2.SQL语句部分

使用到pymysql这个模块,Python默认没有安装,可以在命令窗口运行pip命令进行安装pip install pymysql

import pymysql
# 数据库连接配置
config = {
    "host": "127.0.0.1",  # 数据库地址
    "port": 3306,  # 数据库端口
    "user": "python",  # 数据库用户名
    "password": "123456",  # 数据库密码
    "database": "test"  # 数据库名
}
db = pymysql.connect(**config)
cursor = db.cursor()
cursor.execute("SELECT VERSION()")
data = cursor.fetchone()
print("Database version : %s " % data)


# 增加
def add(datas):
    sql = "INSERT INTO info(username,password,phone) VALUES (%(username)s,%(password)s,%(phone)s)"
    cursor.execute(sql, datas)
    return True


# 查找
def find(username, phone):
    sql_1 = "SELECT * FROM info WHERE BINARY username=%s"
    cursor.execute(sql_1, username)
    str1 = cursor.fetchone()
    sql_2 = "SELECT * FROM info WHERE phone=%s"
    cursor.execute(sql_2, phone)
    str2 = cursor.fetchone()
    return str1, str2


# 更改
def alter(datas, num):
    if num == 0:
        sql_1 = "UPDATE info SET username=%(username)s WHERE id=%(id)s"
        cursor.execute(sql_1, datas)
        return True
    elif num == 1:
        sql_2 = "UPDATE info SET password=%(password)s WHERE id=%(id)s"
        cursor.execute(sql_2, datas)
        return True
    elif num == 2:
        sql_3 = "UPDATE info SET phone=%(phone)s WHERE id=%(id)s"
        cursor.execute(sql_3, datas)
        return True

# 删除
def delete(datas):
    sql = "DELETE FROM info WHERE id=%s"
    cursor.execute(sql, datas)
    return True

三、效果

此文章是我在学校上课时的一些笔记,有错误还请多多包涵,仅供参考
作者:404_502
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0协议。转载请注明文章地址及作者
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇