Python操作MySQL简单实现方法
作者:shichen2014 时间:2024-01-19 04:45:44
本文实例讲述了Python操作MySQL简单实现方法。分享给大家供大家参考。具体分析如下:
一、安装:
安装MySQL
安装MySQL不用多说了,下载下来安装就是,没有特别需要注意的地方。
一个下载地址:点击打开链接
二、示例:
# coding=utf-8
import MySQLdb
#查询数量
def Count(cur):
count=cur.execute('select * from Student')
print 'there has %s rows record' % count
#插入
def Insert(cur):
sql = "insert into Student(ID,Name,Age,Sex)values(%s,%s,%s,%s)"
param = (2,'xiaoming',24,'boy')
cur.execute(sql,param)
#查询
def Select(cur):
n = cur.execute("select * from Student")
print "------"
for row in cur.fetchall():
for r in row:
print r
print "------"
#更新
def Update(cur):
sql = "update Student set Name = %s where ID = 2"
param = ("xiaoxue")
count = cur.execute(sql,param)
#删除
def Delete(cur):
sql = "delete from Student where Name = %s"
param =("xiaoxue")
n = cur.execute(sql,param)
try:
conn=MySQLdb.connect(host='localhost',user='root',passwd='123456',db='python',port=3306)
cur=conn.cursor()
#数量
Count(cur)
#查询
Select(cur)
#插入
Insert(cur)
print "插入之后"
#查询
Select(cur)
#更新
Update(cur)
print "更新之后"
#查询
Select(cur)
#删除
Delete(cur)
print "删除之后"
#查询
Select(cur)
cur.close()
conn.close()
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
希望本文所述对大家的Python程序设计有所帮助。
标签:Python,MySQL
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python中的延迟绑定原理详解
2022-07-08 16:18:35
Python map和reduce函数用法示例
2022-06-21 09:02:25
详解如何用OpenCV + Python 实现人脸识别
2021-07-07 19:22:35
![](https://img.aspxhome.com/file/2023/4/76864_0s.jpg)
javascript 函数声明与函数表达式的区别介绍
2024-04-23 09:08:50
为2021年的第一场雪锦上添花:用matplotlib绘制雪花和雪景
2022-02-01 23:19:56
![](https://img.aspxhome.com/file/2023/2/130832_0s.png)
SQLServer2005 没有日志文件(*.ldf) 只有数据文件(*.mdf) 恢复数据库的方法
2024-01-24 00:15:37
Python中文竖排显示的方法
2022-12-31 20:26:03
使用python实现个性化词云的方法
2021-08-27 03:46:59
![](https://img.aspxhome.com/file/2023/7/82917_0s.png)
Python 里最强的地图绘制神器
2023-07-17 12:36:43
![](https://img.aspxhome.com/file/2023/6/95416_0s.jpg)
Python基于回溯法子集树模板解决m着色问题示例
2023-11-14 12:22:59
![](https://img.aspxhome.com/file/2023/3/109343_0s.png)
python无限生成不重复(字母,数字,字符)组合的方法
2021-02-15 14:08:49
win10 安装mysql 8.0.18-winx64的步骤详解
2024-01-24 06:54:04
![](https://img.aspxhome.com/file/2023/4/70384_0s.jpg)
python中Matplotlib绘制直线的实例代码
2022-09-24 18:27:52
python实现自动重启本程序的方法
2022-07-18 14:16:19
![](https://img.aspxhome.com/file/2023/5/68455_0s.jpg)
如何学习Python time模块
2023-07-30 17:14:59
利用Python演示数型数据结构的教程
2022-04-29 07:05:40
Python中typing模块的具体使用
2022-03-25 13:02:08
PyInstaller运行原理及常用操作详解
2022-09-15 03:57:26
Python实现通过文件路径获取文件hash值的方法
2023-10-27 21:41:20
Python读取txt内容写入xls格式excel中的方法
2023-08-31 22:29:17