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 06 Python
在Python的Django框架中编写编译函数
Jul 20 Python
python爬虫_微信公众号推送信息爬取的实例
Oct 23 Python
利用Python进行异常值分析实例代码
Dec 07 Python
Python中if elif else及缩进的使用简述
May 31 Python
python中单例常用的几种实现方法总结
Oct 13 Python
Python数据集切分实例
Dec 08 Python
python中有关时间日期格式转换问题
Dec 25 Python
python实现二分类和多分类的ROC曲线教程
Jun 15 Python
python 5个顶级异步框架推荐
Sep 09 Python
Python加载数据的5种不同方式(收藏)
Nov 13 Python
python3判断IP地址的方法
Mar 04 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
JS实现php的伪分页
2008/05/25 PHP
php站内搜索并高亮显示关键字的实现代码
2011/12/29 PHP
PHP之浮点数计算比较以及取整数不准确的解决办法
2015/07/29 PHP
WordPress开发中的get_post_custom()函数使用解析
2016/01/04 PHP
thinkPHP5.0框架开发规范简介
2017/03/25 PHP
PHP结合jquery ajax实现上传多张图片,并限制图片大小操作示例
2019/03/01 PHP
PHP使用PDO实现mysql防注入功能详解
2019/12/20 PHP
jQueryUI的Dialog的简单封装
2010/06/07 Javascript
Javascript匿名函数的一种应用 代码封装
2010/06/27 Javascript
javascript 二进制运算技巧解析
2012/11/27 Javascript
js中关于一个分号的崩溃示例
2013/11/11 Javascript
值得分享的轻量级Bootstrap Table表格插件
2016/05/30 Javascript
javascript如何创建对象
2016/08/29 Javascript
DOM 事件的深入浅出(二)
2016/12/05 Javascript
客户端(vue框架)与服务器(koa框架)通信及服务器跨域配置详解
2017/08/26 Javascript
JS实现数组简单去重及数组根据对象中的元素去重操作示例
2018/01/05 Javascript
详解vue组件基础
2018/05/04 Javascript
p5.js绘制旋转的正方形
2019/10/23 Javascript
[01:50]WODOTA制作 DOTA2中文宣传片《HERO》
2013/04/28 DOTA
[06:33]3.19 DOTA2发布会 海涛、冷冷、2009见证希望
2014/03/21 DOTA
python的dataframe转换为多维矩阵的方法
2018/04/11 Python
python三大神器之fabric使用教程
2019/06/10 Python
python连接PostgreSQL数据库的过程详解
2019/09/18 Python
使用python+whoosh实现全文检索
2019/12/09 Python
PyQt5高级界面控件之QTableWidget的具体使用方法
2020/02/23 Python
在python中list作函数形参,防止被实参修改的实现方法
2020/06/05 Python
Python 无限级分类树状结构生成算法的实现
2021/01/21 Python
python元组拆包实现方法
2021/02/28 Python
HTML5 Canvas实现图片缩放、翻转、颜色渐变的代码示例
2016/02/28 HTML / CSS
Canvas实现保存图片到本地的示例代码
2018/06/28 HTML / CSS
学校食品安全实施方案
2014/06/14 职场文书
预备党员学习十八届三中全会精神思想汇报
2014/09/13 职场文书
碧霞祠导游词
2015/02/09 职场文书
2015学校师德师风工作总结
2015/04/22 职场文书
工作调动申请报告
2015/05/18 职场文书
廉政党课工作报告案例
2019/06/21 职场文书