python使用pymysql模块操作MySQL


Posted in Python onJune 16, 2021

实例一:插入数据

python使用pymysql模块操作MySQL

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()

python使用pymysql模块操作MySQL

成功插入数据

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

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()

python使用pymysql模块操作MySQL

实例三:根据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()

python使用pymysql模块操作MySQL

实例四:修改数据

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

python使用pymysql模块操作MySQL

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()

python使用pymysql模块操作MySQL

实例五:删除数据

这里是根据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 相关文章推荐
Django imgareaselect手动剪切头像实现方法
May 26 Python
解析Python中的__getitem__专有方法
Jun 27 Python
Python实现字典排序、按照list中字典的某个key排序的方法示例
Dec 18 Python
python交互界面的退出方法
Feb 16 Python
运用Python的webbrowser实现定时打开特定网页
Feb 21 Python
Python空间数据处理之GDAL读写遥感图像
Aug 01 Python
Python使用Slider组件实现调整曲线参数功能示例
Sep 06 Python
关于Tensorflow使用CPU报错的解决方式
Feb 05 Python
python用pip install时安装失败的一系列问题及解决方法
Feb 24 Python
彻底搞懂 python 中文乱码问题(深入分析)
Feb 28 Python
Python基于QQ邮箱实现SSL发送
Apr 26 Python
Python通过Pillow实现图片对比
Apr 29 Python
分析Python感知线程状态的解决方案之Event与信号量
Jun 16 #Python
Python中else的三种使用场景
Jun 16 #Python
Python基础之条件语句详解
教你怎么用Python实现GIF动图的提取及合成
如何理解python接口自动化之logging日志模块
Jun 15 #Python
python基于turtle绘制几何图形
详解Flask开发技巧之异常处理
Jun 15 #Python
You might like
实用函数8
2007/11/08 PHP
php中cookie的使用方法
2014/03/29 PHP
PHP生成指定随机字符串的简单实现方法
2015/04/01 PHP
php判断访问IP的方法
2015/06/19 PHP
简介WordPress中用于获取首页和站点链接的PHP函数
2015/12/17 PHP
Symfony2创建页面实例详解
2016/03/18 PHP
PHP结合Ueditor并修改图片上传路径
2016/10/16 PHP
javascript 带有滚动条的表格,标题固定,带排序功能.
2009/11/13 Javascript
jquery事件机制扩展插件 jquery鼠标右键事件
2011/12/21 Javascript
jquery动画1.加载指示器
2012/08/24 Javascript
javascript确认框的三种使用方法
2013/12/17 Javascript
JQuery获取表格数据示例代码
2014/05/26 Javascript
jquery及js实现动态加载js文件的方法
2016/01/21 Javascript
jQuery Datatable 多个查询条件自定义提交事件(推荐)
2017/08/24 jQuery
JS实现合并json对象的方法
2017/10/10 Javascript
关于TypeScript模块导入的那些事
2018/06/12 Javascript
Jquery的autocomplete插件用法及参数讲解
2019/03/12 jQuery
vue项目移动端实现ip输入框问题
2019/03/19 Javascript
vue2.x 通过后端接口代理,获取qq音乐api的数据示例
2019/10/30 Javascript
详解如何在Javascript中使用Object.freeze()
2020/10/18 Javascript
使用vant的地域控件追加全部选项
2020/11/03 Javascript
python图像处理之镜像实现方法
2015/05/30 Python
简单掌握Python中glob模块查找文件路径的用法
2016/07/05 Python
Python批量更改文件名的实现方法
2017/10/29 Python
Django中的ajax请求
2018/10/19 Python
对python读取CT医学图像的实例详解
2019/01/24 Python
Python实现最常见加密方式详解
2019/07/13 Python
python二元表达式用法
2019/12/04 Python
python打开文件的方式有哪些
2020/06/29 Python
如何使用Django Admin管理后台导入CSV
2020/11/06 Python
苹果台湾官网:Apple台湾
2019/01/05 全球购物
保送生自荐信范文
2015/03/26 职场文书
会计主管竞聘书
2015/09/15 职场文书
妇产科护理心得体会
2016/01/22 职场文书
Python Pandas常用函数方法总结
2021/06/15 Python
Spring Cloud Netflix 套件中的负载均衡组件 Ribbon
2022/04/13 Java/Android