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 Django连接MySQL数据库做增删改查
Nov 07 Python
python爬虫入门教程之点点美女图片爬虫代码分享
Sep 02 Python
python检测是文件还是目录的方法
Jul 03 Python
Django模板变量如何传递给外部js调用的方法小结
Jul 24 Python
浅谈Python Opencv中gamma变换的使用详解
Apr 02 Python
pygame游戏之旅 添加游戏暂停功能
Nov 21 Python
pyqt5 QProgressBar清空进度条的实例
Jun 21 Python
详解python实现小波变换的一个简单例子
Jul 18 Python
python使用writerows写csv文件产生多余空行的处理方法
Aug 01 Python
django-filter和普通查询的例子
Aug 12 Python
Django Admin中增加导出CSV功能过程解析
Sep 04 Python
python 解决微分方程的操作(数值解法)
May 26 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中使用Oracle数据库(2)
2006/10/09 PHP
深入PHP nl2br()格式化输出的详解
2013/06/05 PHP
php中的boolean(布尔)类型详解
2013/10/28 PHP
php 解压rar文件及zip文件的方法
2014/05/05 PHP
JavaScript与C# Windows应用程序交互方法
2007/06/29 Javascript
javascript document.execCommand() 常用解析
2009/12/14 Javascript
改进版通过Json对象实现深复制的方法
2012/10/24 Javascript
jquery制作select列表双向选择示例代码
2014/09/02 Javascript
jQuery检测滚动条是否到达底部
2015/12/15 Javascript
IE6-IE9使用JSON、table.innerHTML所引发的问题
2015/12/22 Javascript
实例详解jQuery Mockjax 插件模拟 Ajax 请求
2016/01/12 Javascript
前端弹出对话框 js实现ajax交互
2016/09/09 Javascript
jquery 动态增加删除行的简单实例(推荐)
2016/10/12 Javascript
简单模拟node.js中require的加载机制
2016/10/27 Javascript
jquery css实现邮箱自动补全
2016/11/14 Javascript
Vue获取DOM元素样式和样式更改示例
2017/03/07 Javascript
Bootstrap table使用方法汇总
2017/11/17 Javascript
浅谈react性能优化的方法
2018/09/05 Javascript
Vue中的v-for指令不起效果的解决方法
2018/09/27 Javascript
封装微信小程序http拦截器过程解析
2019/08/13 Javascript
layui的面包屑或者表单不显示的解决方法
2019/09/05 Javascript
python 图片验证码代码分享
2012/07/04 Python
2款Python内存检测工具介绍和使用方法
2014/06/01 Python
Python中几个比较常见的名词解释
2015/07/04 Python
新手如何快速入门Python(菜鸟必看篇)
2017/06/10 Python
Python使用xlwt模块操作Excel的方法详解
2018/03/27 Python
基于python历史天气采集的分析
2019/02/14 Python
Python&&GDAL实现NDVI的计算方式
2020/01/09 Python
Python模块 _winreg操作注册表
2020/02/05 Python
python GUI库图形界面开发之PyQt5图片显示控件QPixmap详细使用方法与实例
2020/02/27 Python
Python处理mysql特殊字符的问题
2020/03/02 Python
浅谈html5之sse服务器发送事件EventSource介绍
2017/08/28 HTML / CSS
BIBLOO波兰:捷克的一家在线服装店
2018/03/09 全球购物
Clearly新西兰:购买眼镜、太阳镜和隐形眼镜
2018/04/26 全球购物
搭讪开场白台词大全
2015/05/28 职场文书
springboot如何接收application/x-www-form-urlencoded类型的请求
2021/11/02 Java/Android