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 26 Python
Python用zip函数同时遍历多个迭代器示例详解
Nov 14 Python
使用Python对SQLite数据库操作
Apr 06 Python
Python字典及字典基本操作方法详解
Jan 30 Python
pandas进行数据的交集与并集方式的数据合并方法
Jun 27 Python
kafka-python批量发送数据的实例
Dec 27 Python
用Python写一个模拟qq聊天小程序的代码实例
Mar 06 Python
对python 调用类属性的方法详解
Jul 02 Python
Python实现自定义读写分离代码实例
Nov 16 Python
在Python中使用filter去除列表中值为假及空字符串的例子
Nov 18 Python
Python3使用tesserocr识别字母数字验证码的实现
Jan 29 Python
如何Python使用re模块实现okenizer
Apr 30 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
无线电的诞生过程
2021/03/01 无线电
php随机输出名人名言的代码
2012/10/07 PHP
ThinkPHP 模板substr的截取字符串函数详解
2017/01/09 PHP
常用参考资料(手册)下载或者链接
2006/07/22 Javascript
JavaScript 基于原型的对象(创建、调用)
2009/10/16 Javascript
js 模拟实现类似c#下的hashtable的简单功能代码
2010/01/24 Javascript
JS控制表单提交的方法
2015/07/09 Javascript
js面向对象实现canvas制作彩虹球喷枪效果
2016/09/24 Javascript
javascript中this关键字详解
2016/12/12 Javascript
js实现模糊匹配功能
2017/02/15 Javascript
bootstrap fileinput 上传插件的基础使用
2017/02/17 Javascript
React操作真实DOM实现动态吸底部的示例
2017/10/23 Javascript
bootstrap+jquery项目引入文件报错的解决方法
2018/01/22 jQuery
jquery使用FormData实现异步上传文件
2018/10/25 jQuery
基于vue--key值的特殊用处详解
2020/07/31 Javascript
[47:52]DOTA2-DPC中国联赛正赛 iG vs LBZS BO3 第二场 3月4日
2021/03/11 DOTA
python函数返回多个值的示例方法
2013/12/04 Python
Python 自动刷博客浏览量实例代码
2017/06/14 Python
Python绘制3d螺旋曲线图实例代码
2017/12/20 Python
关于Python正则表达式 findall函数问题详解
2018/03/22 Python
PyQt5实现五子棋游戏(人机对弈)
2020/03/24 Python
使用 Python 写一个简易的抽奖程序
2019/12/08 Python
python实现图像拼接功能
2020/03/23 Python
基于plt.title无法显示中文的快速解决
2020/05/16 Python
python爬虫智能翻页批量下载文件的实例详解
2021/02/02 Python
EJB2和EJB3在架构上的不同点
2014/09/29 面试题
电子商务系毕业生自荐信
2014/05/29 职场文书
消防工作实施方案
2014/06/09 职场文书
元旦趣味活动方案
2014/08/22 职场文书
技术经济专业求职信
2014/09/03 职场文书
导师对论文的学术评语
2015/01/04 职场文书
求职信范文怎么写
2015/03/19 职场文书
2015年保育员个人工作总结
2015/05/13 职场文书
pytest配置文件pytest.ini的详细使用
2021/04/17 Python
MySQL 百万级数据的4种查询优化方式
2021/06/07 MySQL
Go语言实现一个简单的并发聊天室的项目实战
2022/03/18 Golang