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代理抓取并验证使用多线程实现
May 03 Python
Python运行报错UnicodeDecodeError的解决方法
Jun 07 Python
Python矩阵常见运算操作实例总结
Sep 29 Python
python3中property使用方法详解
Apr 23 Python
详解如何减少python内存的消耗
Aug 09 Python
django删除表重建的实现方法
Aug 28 Python
pytorch 中pad函数toch.nn.functional.pad()的用法
Jan 08 Python
解决Python在导入文件时的FileNotFoundError问题
Apr 10 Python
Pytorch实现将模型的所有参数的梯度清0
Jun 24 Python
python实现xlwt xlrd 指定条件给excel行添加颜色
Jul 14 Python
Python eval函数原理及用法解析
Nov 14 Python
python实战之90行代码写个猜数字游戏
Apr 22 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
PHP调用MsSQL Server 2012存储过程获取多结果集(包含output参数)的详解
2013/07/03 PHP
详解PHP的Yii框架中自带的前端资源包的使用
2016/03/31 PHP
Laravel中的Blade模板引擎示例详解
2017/10/10 PHP
做网页的一些技巧
2007/02/01 Javascript
Prototype中dom对象方法汇总
2008/09/17 Javascript
jQuery validate 中文API 附validate.js中文api手册
2010/07/31 Javascript
推荐40个非常优秀的jQuery插件和教程【系列三】
2011/11/09 Javascript
JQuery设置时间段下拉选择实例
2014/12/30 Javascript
jQuery实现单击弹出Div层窗口效果(可关闭可拖动)
2015/09/19 Javascript
总结AngularJS开发者最常犯的十个错误
2016/08/31 Javascript
JS函数多个参数默认值指定方法分析
2016/11/28 Javascript
JavaScript中数组的各种操作的总结(必看篇)
2017/02/13 Javascript
javascript简单写的判断电话号码实例
2017/05/24 Javascript
react 父组件与子组件之间的值传递的方法
2017/09/14 Javascript
详解用函数式编程对JavaScript进行断舍离
2017/09/18 Javascript
React操作真实DOM实现动态吸底部的示例
2017/10/23 Javascript
细说webpack源码之compile流程-rules参数处理技巧(1)
2017/12/26 Javascript
使用js实现将后台传入的json数据放在前台显示
2018/08/06 Javascript
JQuery事件冒泡和默认行为代码实例
2020/05/13 jQuery
Vue Render函数原理及代码实例解析
2020/07/30 Javascript
解决vue项目input输入框双向绑定数据不实时生效问题
2020/08/05 Javascript
python opencv 直方图反向投影的方法
2018/02/24 Python
利用Pyhton中的requests包进行网页访问测试的方法
2018/12/26 Python
python制作图片缩略图
2019/04/30 Python
解决.ui文件生成的.py文件运行不出现界面的方法
2019/06/19 Python
Python Pandas 转换unix时间戳方式
2019/12/07 Python
Python 格式化打印json数据方法(展开状态)
2020/02/27 Python
Python绘图之二维图与三维图详解
2020/08/04 Python
python+selenium 简易地疫情信息自动打卡签到功能的实现代码
2020/08/22 Python
matplotlib对象拾取事件处理的实现
2021/01/14 Python
化工工艺专业求职信
2013/09/22 职场文书
2014年后勤管理工作总结
2014/12/01 职场文书
工程进度款催款函
2015/06/24 职场文书
运动会通讯稿600字
2015/07/20 职场文书
pytorch model.cuda()花费时间很长的解决
2021/06/01 Python
用php如何解决大文件分片上传问题
2021/07/07 PHP