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 相关文章推荐
python实现定制交互式命令行的方法
Jul 03 Python
Python多进程编程技术实例分析
Sep 16 Python
scrapy爬虫实例分享
Dec 28 Python
python使用KNN算法识别手写数字
Apr 25 Python
Python获取好友地区分布及好友性别分布情况代码详解
Jul 10 Python
Flask框架学习笔记之使用Flask实现表单开发详解
Aug 12 Python
python实现发送form-data数据的方法详解
Sep 27 Python
导入tensorflow时报错:cannot import name 'abs'的解决
Oct 10 Python
python调用HEG工具批量处理MODIS数据的方法及注意事项
Feb 18 Python
关于Python字符串显示u...的解决方式
Mar 06 Python
完美解决pycharm 不显示代码提示问题
Jun 02 Python
从np.random.normal()到正态分布的拟合操作
Jun 02 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
《斗罗大陆》六翼天使武魂最强,为什么老千家不是上三宗?
2020/03/02 国漫
PHP-MySQL教程归纳总结
2008/06/07 PHP
Yii2使用swiftmailer发送邮件的方法
2016/05/03 PHP
PHP实现QQ、微信和支付宝三合一收款码实例代码
2018/02/19 PHP
thinkphp5修改view到根目录实例方法
2019/07/02 PHP
php+mysql+ajax 局部刷新点赞/取消点赞功能(每个账号只点赞一次)
2020/07/24 PHP
javascript XMLHttpRequest对象全面剖析
2010/04/24 Javascript
js中有关IE版本检测
2012/01/04 Javascript
jquery实现点击弹出层效果的简单实例
2014/03/03 Javascript
jQuery 过滤方法filter()选择具有特殊属性的元素
2014/06/15 Javascript
js数组的基本操作(很全自己整理的)
2014/10/16 Javascript
2014 HTML5/CSS3热门动画特效TOP10
2014/12/07 Javascript
JS实现网站菜单拖拽移位效果的方法
2015/09/24 Javascript
javascript性能优化之DOM交互操作实例分析
2015/12/12 Javascript
Jq通过td获取同行其它列td的方法
2016/10/05 Javascript
BootStrap Table 获取同行不同列元素的方法
2016/12/19 Javascript
微信小程序实战之上拉(分页加载)效果(2)
2017/04/17 Javascript
javascript实现二叉树遍历的代码
2017/06/08 Javascript
jfinal与bootstrap的登出实战详解
2017/11/27 Javascript
layui table单元格事件修改值的方法
2019/09/24 Javascript
对layui数据表格动态cols(字段)动态变化详解
2019/10/25 Javascript
基于JavaScript实现大文件上传后端代码实例
2020/08/18 Javascript
vue+Element-ui实现登录注册表单
2020/11/17 Javascript
[05:04]完美世界携手游戏风云打造 卡尔工作室地图界面篇
2013/04/23 DOTA
10个示例带你掌握python中的元组
2020/11/23 Python
德购商城:德国进口直邮商城
2017/06/13 全球购物
施华洛世奇中国官网:SWAROVSKI中国
2020/06/16 全球购物
董事长岗位职责
2013/11/30 职场文书
超市国庆节促销方案
2014/02/20 职场文书
大队干部竞选演讲稿
2014/04/28 职场文书
党的群众路线教育实践活动对照检查材料
2014/09/22 职场文书
社区四风存在问题及整改措施
2014/10/26 职场文书
公司行政主管岗位职责
2015/04/09 职场文书
军事理论课感想
2015/08/11 职场文书
golang内置函数len的小技巧
2021/07/25 Golang