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设计模式之观察者模式实例
Apr 26 Python
Python中利用函数装饰器实现备忘功能
Mar 30 Python
举例讲解Python中的迭代器、生成器与列表解析用法
Mar 20 Python
Python 类与元类的深度挖掘 I【经验】
May 06 Python
python anaconda 安装 环境变量 升级 以及特殊库安装的方法
Jun 21 Python
python学习之matplotlib绘制散点图实例
Dec 09 Python
对python多线程中互斥锁Threading.Lock的简单应用详解
Jan 11 Python
python网络编程:socketserver的基本使用方法实例分析
Apr 09 Python
使用openCV去除文字中乱入的线条实例
Jun 02 Python
python安装后的目录在哪里
Jun 21 Python
python中pyqtgraph知识点总结
Jan 26 Python
Python 批量下载阴阳师网站壁纸
May 19 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
基于文本的搜索
2006/10/09 PHP
傻瓜化配置PHP环境――Appserv
2006/12/13 PHP
PHP中isset与array_key_exists的区别实例分析
2015/06/02 PHP
php实现简单爬虫的开发
2016/03/28 PHP
PHP简单实现DES加密解密的方法
2016/07/12 PHP
php弹出提示框的是实例写法
2019/09/26 PHP
JavaScript 编程引入命名空间的方法与代码
2007/08/13 Javascript
Javascript的一种模块模式
2010/09/08 Javascript
JS getAttribute和setAttribute(取得和设置属性)的使用介绍
2013/07/10 Javascript
在父页面得到zTree已选中的节点的方法
2015/02/12 Javascript
深入理解JS的事件绑定、事件流模型
2018/05/13 Javascript
vscode中vue-cli项目es-lint的配置方法
2018/07/30 Javascript
Bootbox将后台JSON数据填充Form表单的实例代码
2018/09/10 Javascript
vue拖拽组件使用方法详解
2018/12/01 Javascript
基于vue.js实现分页查询功能
2018/12/29 Javascript
ES6使用 Array.includes 处理多重条件用法实例分析
2020/03/02 Javascript
el-table树形表格表单验证(列表生成序号)
2020/05/31 Javascript
Python访问MySQL封装的常用类实例
2014/11/11 Python
Python自定义主从分布式架构实例分析
2016/09/19 Python
详解常用查找数据结构及算法(Python实现)
2016/12/09 Python
对python3标准库httpclient的使用详解
2018/12/18 Python
Python Django Cookie 简单用法解析
2019/08/13 Python
Python pip 安装与使用(安装、更新、删除)
2019/10/06 Python
python区分不同数据类型的方法
2019/10/14 Python
django ajax发送post请求的两种方法
2020/01/05 Python
python定义类的简单用法
2020/07/24 Python
详解Python openpyxl库的基本应用
2021/02/26 Python
添柏岚英国官方网站:Timberland英国
2019/11/28 全球购物
大学生的自我鉴定范文
2014/01/21 职场文书
会计专业求职信
2014/08/10 职场文书
2014年国庆节广播稿
2014/09/19 职场文书
重阳节演讲稿:尊敬帮助老人 弘扬传统美德
2014/09/25 职场文书
2015年建筑工程工作总结
2015/05/13 职场文书
详解Spring事件发布与监听机制
2021/06/30 Java/Android
Python 可迭代对象 iterable的具体使用
2021/08/07 Python
spring注解 @PropertySource配置数据源全流程
2022/03/25 Java/Android