python使用pymysql模块操作MySQL

 更新时间:2021年06月15日 17:31:20   作者:头痛的可达鸭Z  
本文讲述了python操作mysql基础实例展示,包含pymysql的使用,tkinter的使用,感兴趣的朋友可以参考下

实例一:插入数据

import pymysql
import tkinter as tk

conn = pymysql.connect(host='localhost', user='root', passwd='root', db='okzl', charset='utf8')

master = tk.Tk()
master.title("插入供应商信息")
master.geometry('350x300')
tk.Label(master, text='cName').place(x=30,y=10)
tk.Label(master, text='address').place(x=30,y=40)
tk.Label(master, text='linkman').place(x=30,y=70)
tk.Label(master, text='linkPhone').place(x=30,y=100)
tk.Label(master, text='credit').place(x=30,y=130)
tk.Label(master, text='remark').place(x=30,y=160)
in1=tk.Entry(master, width=30).place(x=100,y=10)
in2=tk.Entry(master, width=30).place(x=100,y=40)
in3=tk.Entry(master, width=30).place(x=100,y=70)
in3=tk.Entry(master, width=30).place(x=100,y=100)
in3=tk.Entry(master, width=30).place(x=100,y=130)
in3=tk.Entry(master, width=30).place(x=100,y=160)

def insert():
    cur = conn.cursor()  # 伸出手
    sql1 = "insert into pro(cName,address,linkman,linkPhone,credit,remark) values(%s,%s,%s,%s,%s,%s)"
    temp2 = ( )
    cur.execute(sql1, temp2)
    conn.commit()
    cur.close()

tk.Button(master,text='插入',width=8,command=insert).place(x=140,y=220)

master.mainloop()
conn.close()

成功插入数据

实例二:获取某个表全部数据

import pymysql

conn = pymysql.connect(host='localhost', user='root', passwd='root', db='okzl', charset='utf8')
cur = conn.cursor()

cur.execute('select * from pro')
data = cur.fetchall()

cur.close()
print(data)
conn.close()

实例三:根据cName模糊搜索

import pymysql
import tkinter as tk

conn = pymysql.connect(host='localhost', user='root', passwd='root', db='okzl', charset='utf8')  # 连接数据库

master = tk.Tk()
master.title("搜索某客户信息")
master.geometry('350x300')

e = tk.Entry(master)
e.pack(padx=20, pady=20)


def tosearch():
    cur = conn.cursor()
    temp2 = (e.get(), "%" + e.get() + "%")
    cur.execute("select * from pro where cName like %s or cName like %s ", temp2)
    data = cur.fetchall()
    cur.close()
    print(data)


tk.Button(master, text='搜索', width=8, command=tosearch).pack(padx=20, pady=50)

master.mainloop()

conn.close()

实例四:修改数据

根据数据库自动给数据生成的id来确认目标和修改数据

import pymysql
import tkinter as tk

conn = pymysql.connect(host='localhost', user='root', passwd='root', db='okzl', charset='utf8')

master = tk.Tk()
master.title("修改供应商信息")
master.geometry('350x300')
tk.Label(master, text='cName').place(x=30,y=10)
tk.Label(master, text='address').place(x=30,y=40)
tk.Label(master, text='linkman').place(x=30,y=70)
tk.Label(master, text='linkPhone').place(x=30,y=100)
tk.Label(master, text='credit').place(x=30,y=130)
tk.Label(master, text='remark').place(x=30,y=160)
tk.Label(master, text='目标id').place(x=30,y=190)
in1=tk.Entry(master, width=30)
in1.place(x=100,y=10)
in2=tk.Entry(master, width=30)
in2.place(x=100,y=40)
in3=tk.Entry(master, width=30)
in3.place(x=100,y=70)
in4=tk.Entry(master, width=30)
in4.place(x=100,y=100)
in5=tk.Entry(master, width=30)
in5.place(x=100,y=130)
in6=tk.Entry(master, width=30)
in6.place(x=100,y=160)
in7=tk.Entry(master, width=30)
in7.place(x=100,y=190)

def update():
    cur = conn.cursor()  # 伸出手
    sql1 = "update pro set cName=%s, address=%s,linkman=%s,linkPhone=%s,credit=%s,remark=%s where id=%s"
    temp2 = (in1.get(),in2.get(),in3.get(),in4.get(),in5.get(),in6.get(),in7.get())
    cur.execute(sql1, temp2)
    conn.commit()
    cur.close()


tk.Button(master,text='确认修改',width=8,command=update).place(x=140,y=220)

master.mainloop()
conn.close()

实例五:删除数据

这里是根据id删除

sql1 = "delete from pro where id=%s"
temp1 = str(n)
cur.execute(sql1, temp1)
conn.commit()
cur.close()

上述实例均为基础实现操作举例,实际操作中可根据需求更改程序和sql语句实现目标效果

以上就是python使用pymysql模块操作MySQL的详细内容,更多关于python 用pymysql操作MySQL的资料请关注脚本之家其它相关文章!

相关文章

  • python交互模式下输入换行/输入多行命令的方法

    python交互模式下输入换行/输入多行命令的方法

    这篇文章主要介绍了python交互模式下输入换行/输入多行命令的方法,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-07-07
  • python安装pytorch方式

    python安装pytorch方式

    这篇文章主要介绍了python安装pytorch方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • Python中atexit模块的基本使用示例

    Python中atexit模块的基本使用示例

    这篇文章主要介绍了Python中atexit模块的基本使用示例,示例代码基于Python2.x版本,注意其和Python3的兼容性,需要的朋友可以参考下
    2015-07-07
  • Python开发必须掌握的Pip使用全攻略

    Python开发必须掌握的Pip使用全攻略

    在这篇文章中,我们将深入探讨Python的主要包管理工具——Pip,包括Pip的基本概念、安装和配置、中国国内镜像源的使用等,需要的可以参考一下
    2023-07-07
  • 查看Django和flask版本的方法

    查看Django和flask版本的方法

    今天小编就为大家分享一篇查看Django和flask版本的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-05-05
  • python Pexpect 实现输密码 scp 拷贝的方法

    python Pexpect 实现输密码 scp 拷贝的方法

    今天小编就为大家分享一篇python Pexpect 实现输密码 scp 拷贝的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-01-01
  • Python3利用SMTP协议发送E-mail电子邮件的方法

    Python3利用SMTP协议发送E-mail电子邮件的方法

    SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。下面这篇文章主要给大家介绍了关于Python3如何利用SMTP协议发送E-mail电子邮件的方法,需要的朋友可以参考下。
    2017-09-09
  • Python实现多态、协议和鸭子类型的代码详解

    Python实现多态、协议和鸭子类型的代码详解

    问起面向对象的三大特性,几乎每个人都能对答如流:封装、继承、多态。今天我们就要来说一说Python实现多态、协议和鸭子类型,感兴趣的朋友跟随小编一起看看吧
    2019-05-05
  • python感知机实现代码

    python感知机实现代码

    这篇文章主要为大家详细介绍了python感知机实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • Django serializer优化类视图的实现示例

    Django serializer优化类视图的实现示例

    这篇文章主要介绍了Django serializer优化类视图的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07

最新评论