最强python连接mysql仓库管理系统

import pymysql.cursors
from tkinter import ttk
import tkinter as tk
import tkinter.font as tkFont
from tkinter import *
import tkinter.messagebox as messagebox
import time


class logincheck:
    def __init__(self,parent_window):
        parent_window.update()
        parent_window.destroy()
        self.window=tk.Tk()
        self.window.title("登录界面")
        self.window.geometry('700x600+70+50')
        def getTime():
            timeStr=time.strftime('%H:%M:%S')
            Rtime.configure(text=timeStr)
            self.window.after(1000, getTime)
        Rtime= Label(self.window,text='')
        Rtime.pack(pady=25)
        getTime()
        label = Label(self.window, text="新一佳仓储商品管理系统登录", font=("楷体", 30))
        label.pack(pady=10)
        self.var_username=StringVar()
        self.var_pwd=StringVar()

        self.right_top_username_label = Label(text="用户名:", font=('楷体', 15)).pack(pady=15)
        self.right_top_username_entry = Entry(textvariable=self.var_username, font=('楷体', 15)).pack()

        self.right_top_pwd_label = Label(text="密码:", font=('楷体', 15)).pack(pady=15)
        self.right_top_pwd_entry = Entry(textvariable=self.var_pwd, font=('楷体', 15)).pack()

        self.right_top_button1 = ttk.Button(text='确定', width=20, command=self.new_row).pack(pady=30)
        self.right_top_button2 = ttk.Button(text='返回', width=20, command=self.back).pack()
        self.window.protocol("WM_DELETE_WINDOW", self.back)

        self.username=[]
        self.pwd=[]

        self.window.mainloop()
        db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus")
        cursor = db.cursor()
        sql = "SELECT * FROM 用户登录 WHERE 用户名='%s'"%(self.var_username.get())
        try:
            cursor.execute(sql)
            results = cursor.fetchall()
            for row in results:
                self.username.append(row[0])
                self.pwd.append(row[1])
        except:
            print("Error: unable to fetch data")
            messagebox.showinfo('警告!', '数据库连接失败!')
        db.close()

    def back(self):
        self.window.destroy()

    def new_row(self):
        if self.var_username.get() != '' and self.var_pwd.get() != '' and self.var_username.get()=='test' and self.var_pwd.get()=='123':
            db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus")
            cursor= db.cursor()
            sql = "SELECT * FROM 用户登录 WHERE 用户名='%s'"%(self.var_username.get())
            try:
                cursor.execute(sql)
                startpage(self.window)
            except:
                        db.rollback()
                        messagebox.showinfo('警告!', '数据库连接失败!')
            db.close()
        else:
                    messagebox.showinfo('提示!', '请填写正确的用户信息')






class startpage:
    def __init__(self,parent_window):
        parent_window.update()
        parent_window.destroy()
        self.window=tk.Tk()
        self.window.title("新一佳仓储商品管理系统")
        self.window.geometry('1200x600+70+50')
        def getTime():
            timeStr=time.strftime('%H:%M:%S')
            Rtime.configure(text=timeStr)
            self.window.after(1000, getTime)
        Rtime= Label(self.window,text='')
        Rtime.pack(pady=25)
        getTime()
        label= Label(self.window,text="新一佳仓储商品管理系统",font=("楷体",30))
        label.pack(pady=10)
        Button(self.window,text="添加商品",font=tkFont.Font(size=16),command=lambda: xinjian(self.window),width=20,height=2,fg='white',bg='gray').place(x=100,y=300)
        Button(self.window,text="依据编号查询",font=tkFont.Font(size=16), command=lambda: cangkucha(self.window), width=20,height=2, fg='white', bg='gray').place(x=400, y=300)
        Button(self.window, text="商品出库", font=tkFont.Font(size=16), command=lambda:shanchu(self.window), width=20,height = 2, fg = 'white', bg = 'gray').place(x=100, y=400)
        Button(self.window, text="退出系统", font=tkFont.Font(size=16), command=self.window.destroy, width=20,height=2, fg='white', bg='gray').place(x=700, y=700)
        Button(self.window, text="修改商品信息", font=tkFont.Font(size=16), command=lambda:xiugai(self.window),width=20,height=2,fg='white',bg='gray').place(x=700,y=300)
        Button(self.window, text="依据商品名查询", font=tkFont.Font(size=16), command=lambda:cangkucha2(self.window), width=20,height=2, fg='white', bg='gray').place(x=400, y=400)
        self.window.mainloop()

class xinjian:
    def __init__(self,parent_window):
        parent_window.destroy()
        self.window=tk.Tk()
        self.window.title("添加商品")
        self.window.geometry('700x600+70+50')
        self.top_title=Label(self.window,text="添加商品",bg='SkyBlue', font=('楷体', 20), width=70, height=2)
        self.top_title.pack()

        self.var_id = StringVar()
        self.var_name = StringVar()
        self.var_gender = StringVar()
        self.var_age = StringVar()
        self.var_num = StringVar()

        self.right_top_id_label = Label(text="商品编号:", font=('楷体', 15)).pack(pady=15)
        self.right_top_id_entry = Entry(textvariable=self.var_id, font=('楷体', 15)).pack()

        self.right_top_name_label =Label(text="商品名:", font=('楷体', 15)).pack(pady=15)
        self.right_top_name_entry = Entry(textvariable=self.var_name, font=('楷体', 15)).pack()

        self.right_top_gender_label = Label(text="商品类别:", font=('楷体', 15)).pack(pady=15)
        self.right_top_gender_entry = Entry(textvariable=self.var_gender, font=('楷体', 15)).pack()

        self.right_top_age_label = Label(text="商品数量:", font=('楷体', 15)).pack(pady=15)
        self.right_top_age_entry = Entry(textvariable=self.var_age, font=('楷体', 15)).pack()

        self.right_top_num_label = Label(text="商品供应商:",font=('楷体',15)).pack(pady=15)
        self.right_top_num_entry = Entry(textvariable=self.var_num, font=('楷体', 15)).pack()


        self.right_top_button1 = ttk.Button(text='确定', width=20, command=self.new_row).pack(pady=30)
        self.right_top_button2 = ttk.Button(text='返回', width=20, command=self.back).pack()
        self.window.protocol("WM_DELETE_WINDOW", self.back)  # 捕捉右上角关闭点击

        self.id=[]
        self.name=[]
        self.gender=[]
        self.age=[]
        self.num=[]
        db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus")
        cursor = db.cursor()
        sql = "SELECT * FROM 商品表"
        try:
            cursor.execute(sql)
            results=cursor.fetchall()
            for row in results:
                self.id.append(row[0])
                self.name.append(row[1])
                self.gender.append(row[2])
                self.age.append(row[3])
                self.num.append(row[4])
        except:
            print("Error: unable to fetch data")
            messagebox.showinfo('警告!', '数据库连接失败!')
        db.close()


    def back(self):
        startpage(self.window)
    def new_row(self):
        if self.var_id.get() != '' and self.var_name.get() != '' and self.var_gender.get() != '' and self.var_age.get() != '':
            db=pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus")
            cursor = db.cursor()  # 使用cursor()方法获取操作游标
            sql= "INSERT  INTO 商品表(商品编号,商品名,商品类别,商品数量,商品供应商) VALUES('%s','%s','%s','%s','%s')"%(self.var_id.get(), self.var_name.get(),self.var_gender.get(), self.var_age.get(), self.var_num.get())
            try:
                cursor.execute(sql)
                db.commit()
                messagebox.showinfo('提示!', '添加成功!')
            except:
                db.rollback()
                messagebox.showinfo('警告!', '数据库连接失败!')
            db.close()
        else:
            messagebox.showinfo('提示!', '请填写客户信息')
class kehudan:
    def __init__(self,parent_window):
        parent_window.destroy()  # 销毁子界面
        self.window = tk.Tk()
        self.window.title('商品清单')
        self.window.geometry('1200x600+70+50')
        db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus")
        cursor = db.cursor()  # 使用cursor()方法获取操作游标
        sql = "SELECT * FROM 商品表"
        try:
            cursor.execute(sql)  # 执行sql语句
            results = cursor.fetchall()
            for row in results:
                self.id = '商品编号: ' + row[0]
                self.name = '商品名: ' + row[1]
                self.gender = '商品类别: ' + row[2]
                self.age = '商品数量: ' + row[3]
                self.num = '商品供应商:' + row[4]

                db.commit()
                Label(self.window, text=self.id + "\t" + self.name + "\t" + self.gender + "\t" +self.age + "\t" +self.num, font=('楷体', 18)).pack(pady=5)
        except:
            db.rollback()  # 发生错误时回滚
            messagebox.showinfo('警告!', '数据库连接失败!')
        db.close()  # 关闭数据库连接
        self.right_top_button4 = ttk.Button(text='返回', width=20, command=self.back).pack()
        self.window.protocol("WM_DELETE_WINDOW", self.back)
    def back(self):
        cangkucha(self.window)

class cangkucha:
    def __init__(self, parent_window):
        parent_window.destroy()
        self.window = tk.Tk()
        self.window.title('依据商品编号查询')
        self.window.geometry('700x600+70+50')
        self.student_id = StringVar()
        self.id = '商品编号:' + ''
        self.name = '商品名:' + ''
        self.gender = '商品类别:' + ''
        self.age = '商品数量:' + ''
        self.num = '商品供应商:' + ''

        Button(self.window, text="商品清单", font=tkFont.Font(size=12), command=lambda: kehudan(self.window), width=20,
               height=2, fg='white', bg='gray').place(x=20, y=70)
        self.right_top_name_label = Label(text="商品查询", font=('楷体', 15)).pack(pady=15)
        self.right_top_name_entry = Entry(textvariable=self.student_id, font=('楷体', 15)).pack(pady=30)

        self.right_top_button3 = ttk.Button(text='确定', width=20, command=self.new_row).pack(pady=30)
        self.right_top_button4 = ttk.Button(text='返回', width=20, command=self.back).pack()
        self.window.protocol("WM_DELETE_WINDOW", self.back)

        # 打开数据库连接
        db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus")
        cursor = db.cursor()
        sql = "SELECT * FROM 商品表 WHERE 商品编号='%s'" % (self.student_id.get()) # SQL 查询语句
        try:
            # 执行SQL语句
            cursor.execute(sql)
            # 获取所有记录列表
            results = cursor.fetchall()
            for row in results:
                self.id = '商品编号:' + row[0]
                self.name = '商品名:' + row[1]
                self.gender = '商品类别:' + row[2]
                self.age = '商品数量:' + row[3]
                self.num ='商品供应商:' + row[4]
        except:
            print("Error: unable to fetch data")
        db.close()

    def back(self):
        startpage(self.window)

    def new_row(self):
        if self.student_id.get() != '':
            db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus")
            cursor = db.cursor()
            sql = "SELECT * FROM 商品表 where 商品编号 = '%s'" % (self.student_id.get())  # SQL 插入语句
            try:
                cursor.execute(sql)
                results = cursor.fetchall()
                for row in results:
                    self.id = '商品编号:' + row[0]
                    self.name = '商品名:' + row[1]
                    self.gender = '商品类别:' + row[2]
                    self.age = '商品数量:' + row[3]
                    self.num ='商品供应商:' + row[4]
                db.commit()
                label = tk.Label(self.window, text='商品信息查看', bg='SkyBlue', font=('楷体', 20), width=70, height=2)
                label.pack(pady=20)
                Label(self.window, text=self.id, font=('楷体', 18)).pack(pady=5)
                Label(self.window, text=self.name, font=('楷体', 18)).pack(pady=5)
                Label(self.window, text=self.gender, font=('楷体', 18)).pack(pady=5)
                Label(self.window, text=self.age, font=('楷体', 18)).pack(pady=5)
                Label(self.window, text=self.num, font=('楷体', 18)).pack(pady=5)
                Button(self.window, text="返回首页", width=8, font=tkFont.Font(size=12), command=self.back_1).pack(pady=150)
                self.window.protocol("WM_DELETE_WINDOW", self.back_1)
                self.window.mainloop()
            except:
                db.rollback()
                messagebox.showinfo('提示', '数据库连接失败!')

            db.close()
        else:
            messagebox.showinfo('提示', '请填写商品信息!')


    def back_1(self):
        cangkucha(self.window)
class cangkucha2:
    def __init__(self, parent_window):
        parent_window.destroy()
        self.window = tk.Tk()
        self.window.title('依据商品名查询')
        self.window.geometry('700x600+70+50')

        self.student_id = StringVar()
        self.student_name = StringVar()
        self.id = '商品编号:' + ''
        self.name = '商品名:' + ''
        self.gender = '商品类别:' + ''
        self.age = '商品数量:' + ''
        self.num = '商品供应商:' + ''

        Button(self.window, text="商品清单", font=tkFont.Font(size=12), command=lambda: kehudan(self.window), width=20,
               height=2, fg='white', bg='gray').place(x=20, y=70)
        self.right_top_name_label = Label(text="商品查询", font=('楷体', 15)).pack(pady=15)
        self.right_top_name_entry = Entry(text="商品编号", textvariable=self.student_name, font=('楷体', 15)).pack(pady=30)

        self.right_top_button3 = ttk.Button(text='确定', width=20, command=self.new_row).pack(pady=30)
        self.right_top_button4 = ttk.Button(text='返回', width=20, command=self.back).pack()
        self.window.protocol("WM_DELETE_WINDOW", self.back)

        # 打开数据库连接
        db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus")
        cursor = db.cursor()
        sql = "SELECT * FROM 商品表 WHERE 商品名='%s'" % (self.student_name.get()) # SQL 查询语句
        try:
            # 执行SQL语句
            cursor.execute(sql)
            # 获取所有记录列表
            results = cursor.fetchall()
            for row in results:
                self.id = '商品编号:' + row[0]
                self.name = '商品名:' + row[1]
                self.gender = '商品类别:' + row[2]
                self.age = '商品数量:' + row[3]
                self.num ='商品供应商:' + row[4]
        except:
            print("Error: unable to fetch data")
        db.close()

    def back(self):
        startpage(self.window)

    def new_row(self):
        if self.student_name.get() != '':
            db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus")
            cursor = db.cursor()
            sql = "SELECT * FROM 商品表 where 商品名 = '%s'" % (self.student_name.get())  # SQL 插入语句
            try:
                cursor.execute(sql)
                results = cursor.fetchall()
                for row in results:
                    self.id = '商品编号:' + row[0]
                    self.name = '商品名:' + row[1]
                    self.gender = '商品类别:' + row[2]
                    self.age = '商品数量:' + row[3]
                    self.num ='商品供应商:' + row[4]
                db.commit()
                label = tk.Label(self.window, text='商品信息查看', bg='SkyBlue', font=('楷体', 20), width=70, height=2)
                label.pack(pady=20)
                Label(self.window, text=self.id, font=('楷体', 18)).pack(pady=5)
                Label(self.window, text=self.name, font=('楷体', 18)).pack(pady=5)
                Label(self.window, text=self.gender, font=('楷体', 18)).pack(pady=5)
                Label(self.window, text=self.age, font=('楷体', 18)).pack(pady=5)
                Label(self.window, text=self.num, font=('楷体', 18)).pack(pady=5)
                Button(self.window, text="返回首页", width=8, font=tkFont.Font(size=12), command=self.back_1).pack(pady=150)
                self.window.protocol("WM_DELETE_WINDOW", self.back_1)
                self.window.mainloop()
            except:
                db.rollback()
                messagebox.showinfo('提示', '数据库连接失败!')

            db.close()
        else:
            messagebox.showinfo('提示', '请填写商品信息!')


    def back_1(self):
        cangkucha2(self.window)
class shanchu:
    def __init__(self,parent_window):
        parent_window.destroy()

        self.window = tk.Tk()
        self.window.title('删除表')
        self.window.geometry('700x600+70+50')

        self.top_title = Label(self.window, text='删除', bg='SkyBlue', font=('楷体', 20), width=70, height=2)
        self.top_title.pack()

        self.var_id = StringVar()
        self.var_name = StringVar()
        self.var_gender = StringVar()
        self.var_age = StringVar()
        self.var_num= StringVar()
        self.right_top_id_label = Label(text="商品编号", font=('楷体', 15)).pack(pady=15)
        self.right_top_id_entry = Entry(textvariable=self.var_id, font=('楷体', 15)).pack()

        self.right_top_button1 = ttk.Button(text='确定', width=20, command=self.new_row).pack(pady=30)
        self.right_top_button2 = ttk.Button(text='返回', width=20, command=self.back).pack()
        self.window.protocol("WM_DELETE_WINDOW", self.back)

        self.id = []
        self.name = []
        self.gender = []
        self.age = []
        self.num= []
        # 打开数据库连接
        db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus")
        cursor = db.cursor()  # 使用cursor()方法获取操作游标
        sql = "SELECT * FROM 商品表"  # SQL 查询语句
        try:
            # 执行SQL语句
            cursor.execute(sql)
            # 获取所有记录列表
            results = cursor.fetchall()
            for row in results:
                self.id.append(row[0])
                self.name.append(row[1])
                self.gender.append(row[2])
                self.age.append(row[3])
                self.num.append(row[4])
        except:
            print("Error: unable to fetch data")
            messagebox.showinfo('提示', '数据库连接失败!')
        db.close()

    def back(self):
            startpage(self.window)

    def new_row(self):
            if self.var_id.get() != '':
                db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus")
                cursor = db.cursor()
                sql = "DELETE FROM 商品表 WHERE 商品编号 = '%s'" % (self.var_id.get())
                try:
                    cursor.execute(sql)
                    db.commit()
                    messagebox.showinfo('提示!', '删除成功!')
                except:
                    db.rollback()
                    messagebox.showinfo('警告!', '数据库连接失败!')
                db.close()
            else:
                messagebox.showinfo('警告!', '填写删除信息')
class xiugai:
    def __init__(self,parent_window):
        parent_window.destroy()
        self.window = tk.Tk()
        self.window.title('修改信息')
        self.window.geometry('700x600+70+50')
        self.top_title = Label(self.window, text='修改信息', bg='SkyBlue', font=('楷体', 20), width=70, height=2)
        self.top_title.pack()

        self.var_id = StringVar()
        self.var_name = StringVar()
        self.var_gender = StringVar()
        self.var_age = StringVar()
        self.var_num = StringVar()
        self.right_top_id_label = Label(text="商品编号", font=('楷体', 15)).pack(pady=15)
        self.right_top_id_entry = Entry(textvariable=self.var_id, font=('楷体', 15)).pack()

        self.right_top_name_label = Label(text="商品名", font=('楷体', 15)).pack(pady=15)
        self.right_top_name_entry = Entry(textvariable=self.var_name, font=('楷体', 15)).pack()

        self.right_top_gender_label = Label(text="商品类别", font=('楷体', 15)).pack(pady=15)
        self.right_top_gender_entry = Entry(textvariable=self.var_gender, font=('楷体', 15)).pack()

        self.right_top_age_label = Label(text="商品数量", font=('楷体', 15)).pack(pady=15)
        self.right_top_age_entry = Entry(textvariable=self.var_age, font=('楷体', 15)).pack()

        self.right_top_num_label = Label(text="商品供应商", font=('楷体', 15)).pack(pady=15)
        self.right_top_num_entry = Entry(textvariable=self.var_num, font=('楷体', 15)).pack()

        self.right_top_button1 = ttk.Button(text='确定', width=20, command=self.new_row).pack(pady=30)
        self.right_top_button2 = ttk.Button(text='返回', width=20, command=self.back).pack()
        self.window.protocol("WM_DELETE_WINDOW", self.back)
        self.id = []
        self.name = []
        self.gender = []
        self.age = []
        self.num = []
        # 打开数据库连接
        db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus")
        cursor = db.cursor()  # 使用cursor()方法获取操作游标
        sql = "SELECT * FROM 商品表"  # SQL 查询语句
        try:
            # 执行SQL语句
            cursor.execute(sql)
            # 获取所有记录列表
            results = cursor.fetchall()
            for row in results:
                self.id.append(row[0])
                self.name.append(row[1])
                self.gender.append(row[2])
                self.age.append(row[3])
                self.num.append(row[4])
        except:
            print("Error: unable to fetch data")
            messagebox.showinfo('提示', '数据库连接失败!')
        db.close()

    def back(self):
            startpage(self.window)
    def new_row(self):
            if self.var_id.get() != '':
                db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus")
                cursor = db.cursor()
                sql = "UPDATE  商品表 SET 商品编号='%s',商品名='%s',商品类别='%s',商品数量='%s',商品供应商='%s' WHERE 商品编号='%s'" % (self.var_id.get(),self.var_name.get(),self.var_gender.get(),self.var_age.get(),self.var_num.get(),self.var_id.get())


                try:
                    cursor.execute(sql)
                    db.commit()
                    messagebox.showinfo('提示!', '修改成功!')
                except:
                    db.rollback()
                    messagebox.showinfo('警告!', '数据库连接失败!')
                db.close()
            else:
                messagebox.showinfo('警告!', '填写修改信息')



if __name__ == '__main__':
            window = tk.Tk()
            logincheck(window)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Python连接MySQL图书管理系统可以通过以下步骤实现: 1. 安装MySQL数据库PythonMySQL驱动程序(如pymysql)。 2. 创建一个数据库和表格,用于存储图书信息。 3. 在Python中使用pymysql模块连接MySQL数据库。 4. 编写Python代码来实现图书管理系统的各种功能,如添加、删除、修改和查询图书信息。 5. 在Python中使用GUI库(如Tkinter)创建一个用户界面,以便用户可以轻松地使用图书管理系统。 6. 将Python代码打包成可执行文件,以便用户可以在没有Python环境的情况下运行图书管理系统。 以上是Python连接MySQL图书管理系统的基本步骤,具体实现还需要根据实际需求进行调整和完善。 ### 回答2: Python是一种高级编程语言,用于开发各种应用程序和系统。其语法简洁、易于学习和使用,成为了广大开发者的首选之一。同时,MySQL是一种流行的关系型数据库管理系统,其稳定性和可靠性受到开发者的高度认可。在本文中,我们将介绍如何使用Python连接MySQL并开发一个基于MySQL数据库的图书管理系统。 在开发之前,首先需要进行安装相关的软件环境。需要安装PythonMySQL数据库管理系统,我们可以在Python官网和MySQL官网上下载安装包并进行安装。安装之后,我们需要安装pymysql库,pymysqlpythonMySQL数据库操作非常重要的一个库,其提供了基于Python语言的MySQL连接和操作。 连接MySQLPython连接MySQL数据库,需要使用MySQLdb或者pymysql等第三方库。首先,需要导入pymysql库: import pymysql 接下来,需要连接MySQL数据库连接MySQL数据库需要输入数据库主机地址、用户名、密码、数据库名称等信息,并调用connect函数。 # 连接数据库,创建连接对象 db = pymysql.connect(host='localhost',user='root', passwd='123456',db='book_db',charset='utf8') cursor对象 连接MySQL数据库之后,我们需要创建游标对象来执行SQL语句,使用游标对象可以对数据库进行读写操作。在Python中,需要使用pymysql库的cursor()函数创建游标对象。创建cursor()函数后,需要向它传递游标类型,一般使用pymysql.cursors.DictCursor保持数据以词典的形式存储。 创建游标对象: # 创建游标 cursor = db.cursor(pymysql.cursors.DictCursor) 创建表 在我们的图书管理系统中,需要创建图书信息表、读者信息表等。在pymysql中,使用execute()函数执行SQL语句。若要创建表,需要在SQL语句中指定表名、列名、数据类型、约束条件等。 #定义创建表语句 create_book_table_sql = """ CREATE TABLE IF NOT EXISTS book( book_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, book_name VARCHAR(50) NOT NULL, author VARCHAR(50) NOT NULL, price FLOAT NOT NULL, publish_date DATE NOT NULL ) """ # 执行创建表语句 cursor.execute(create_book_table_sql) 插入数据 在我们的图书管理系统中,需要添加图书信息和读者信息等。如果要插入数据,需要在SQL语句中指定表名、列名和对应的值。在pymysql中,使用execute()函数执行SQL语句。 # 定义插入图书数据的SQL语句 insert_sql = """ INSERT INTO book(book_name, author, price,publish_date) VALUES (%s, %s, %s, %s) """ # 执行插入数据的SQL语句 cursor.execute(insert_sql, ('Python编程', 'Guido van Rossum', 99.00,'2021-10-01')) 查询数据 在我们的图书管理系统中,需要查询所有图书信息、特定条件下的图书信息等。如果要查询数据,需要在SQL语句中指定表名、列名和查询条件。在pymysql中,使用execute()函数执行SQL语句。 # 定义查询图书数据的SQL语句 select_sql = """ SELECT * FROM book WHERE book_name=%s """ # 执行查询图书数据的SQL语句 cursor.execute(select_sql, ('Python编程',)) # 获取查询结果 result = cursor.fetchall() 总之,我们可以通过以上的实现,来开发一个完成图书管理系统的Python应用,为图书馆管理员提供方便快捷的图书管理服务。 ### 回答3: Python是一种广泛使用的通用编程语言,而MySQL则是一种流行的关系型数据库管理系统,可以在 Python 中使用丰富的库来连接与操作MySQL数据库,用Python连接MySQL可以实现各种功能,如插入、查询、更新、删除等数据库操作,因此,使用Python连接MySQL图书管理系统是非常可行的。 首先,我们需要安装PythonMySQL数据库,安装完毕后,我们需要安装MySQL连接库,例如PyMySQLMySQL-python等。这些库可以通过pip包管理器进行安装,在Python代码中导入这些库就可以连接MySQL数据库。 假设MySQL中已经创建了一个名为books的数据库,其中有一个名为bookinfo的表,该表包含有书籍的相关信息,包括书名、作者、出版社、价格等,请参考以下示例代码: ``` import pymysql # 连接MySQL数据库 conn = pymysql.connect(host='localhost', port=3306, user='root', password='your_password', db='books', charset='utf8') # 创建游标 cursor = conn.cursor() # 插入数据 insert_sql = "INSERT INTO bookinfo(bookname, author, publisher, price) VALUES('Python编程从入门到实践', 'Eric Matthes', '人民邮电出版社', '89.8元')" cursor.execute(insert_sql) # 查询数据 select_sql = "SELECT * FROM bookinfo" cursor.execute(select_sql) result = cursor.fetchall() for row in result: print(row) # 更新数据 update_sql = "UPDATE bookinfo SET price='99.8元' WHERE bookname='Python编程从入门到实践'" cursor.execute(update_sql) # 删除数据 delete_sql = "DELETE FROM bookinfo WHERE bookname='Python编程从入门到实践'" cursor.execute(delete_sql) # 提交操作 conn.commit() # 关闭游标和连接 cursor.close() conn.close() ``` 以上代码先连接MySQL数据库,然后创建了一个游标,通过游标实现对数据库的增删改查操作,最后提交修改并关闭游标和连接。 需要注意的是,在实际开发中,应当根据实际需要,对连接MySQL的代码进行模块化,加入异常处理等做法,以提高程序的健壮性和可维护性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值