python轻松操纵mysql数据库(详解Python操作MySQL数据库)
python轻松操纵mysql数据库(详解Python操作MySQL数据库)# 4. 在这一步写你的sql语句cursor = db.cursor# 2. 链接MySQL服务器db = pymysql.connect(host='localhost' user='root' password='******' port=3306 db='spiders' charset='utf8')# 3. 创建一个cursor游标对象
本文的重点,就是教会大家,如何用Python来操作MySQL数据库。
1. 通用步骤其实,这里有一个通用步骤,都是写死了的,大家照做就行。
# 1. 导入相关库
import pymysql
# 2. 链接MySQL服务器
db = pymysql.connect(host='localhost' user='root' password='******' port=3306 db='spiders' charset='utf8')
# 3. 创建一个cursor游标对象
cursor = db.cursor
# 4. 在这一步写你的sql语句
sql = 'select version'
# 5. 执行sql语句
cursor.execute(sql)
# 6. 断开连接
db.close
可以看出,整个过程就是第四步那里不同。归根到底,其实就是写sql。
2. 需要特别说明的知识点Ⅰ 各参数说明
db = pymysql.connect(host='localhost' user='root' password='******' port=3306 db='spiders' charset='utf8')
这一行代码很长,里面涉及到好几个参数,这里为大家一一介绍如下:
-
参数 1 :mysql 服务器所在的主机 IP
-
参数 2 :用户名;
-
参数 3 :密码;
-
参数 4 :连接的 mysql 主机的端口,默认是 3306;
-
参数 5 :连接的数据库名;
-
参数 6 :通信采用的编码方式,默认是'gb2312',要求与数据库创建时指定的编码一致,否则中文会乱码;
Ⅱ cursor游标对象
① 创建一个数据表
import pymysql
② 插入数据
import pymysql
db = pymysql.connect(host='192.168.3.47' user='root' password='******' port=3306 db='spiders' charset='utf8')
cursor = db.cursor
# 插入数据
sql = 'insert into students(name age) values(%s %s)'
try:
cursor.execute(sql ('孙悟空' 100000))
db.commit
except:print("插入失败")
db.rollback
db.close
注 1:插入数据一定要用 try…except…语句,因为万一没插入成功,其余代码都无法执行。
注 2:import pymysql,此模块是默认开启mysql的事务功能的,因此,进行“增”、“删”、“改”的时候,一定要使用db.commit提交事务,否则就看不见所插入的数据。
③ 更新数据
import pymysql
db = pymysql.connect(host='192.168.3.47' user='root' password='******' port=3306 db='spiders' charset='utf8')
cursor = db.cursor
# 更新数据
sql = 'update students set age =%s where name=%s'
try:
cursor.execute(sql (30 "郭卫华"))
db.commit
except:
print("插入失败")
db.rollback
db.close
③ 删除操作
import pymysql
db = pymysql.connect(host='192.168.3.47' user='root' password='******' port=3306 db='spiders' charset='utf8')
cursor = db.cursor
# 删除数据
sql = 'delete from students where age=100000'
try:
cursor.execute(sql)
db.commit
except:
print("插入失败")
db.rollback
db.close
④ 查询操作
-
fetchone功能:获取下一个查询结果集,结果集是一个对象。
-
fetchall功能:接收全部返回的行。
import pymysql
db = pymysql.connect(host='192.168.3.47' user='root' password='******' port=3306 db='spiders' charset='utf8')
cursor = db.cursor
# 查询数据
sql = 'select * from students where age>60'
try:
cursor.execute(sql)
reslist = cursor.fetchall
for row in reslist:
print(“%d--%d” %(row[0] row[1] … row[n]))
except:
print("插入失败")
db.rollback
db.close
4. 封装一个类# 注:把下面类写在 studentsql 文件中
import pymysql
class StudentsSql:
def __init__(self host user port dbname charset):
self.host = host
self.user = user
self.port = port
self.dbname = dbname
self.charset = charset
def connet(sef):
self.db = pymysql.connect(self.host self.user self.port
self.dbname self.charset)
def close(self):
self.cursor.close
self.db.close
def get_one(self sql):
res = None
try:
self.connect
self.cursor.execute(sql)
res = self.cursor.fetchone
self.close
except:
print(“查询失败”)
return res
def get_all(self sql):
res = None
try:
self.connect
self.cursor.execute(sql)
res = self.cursor.fetchall
self.close
except:
print(“查询失败”)
return res
def inset(self sql):
return self.__edit(sql)
def update(self sql):
return self.__edit(sql)
def delete(self sql):
return self.__edit(sql)
def __edit(self sql):
count = 0
try:
self.connect
count = self.cursor.execute(sql)
self.db.commit
self.close
except:
print(“事务提交失败”)
self.db.rollback
return count
上述类封装成功,以后只需要调用即可。
from studentsql import StudentsSql
s = StudentsSql(“host='192.168.3.47' user='root' password='******' port=3306 db='spiders' charset='utf8'”)
res = s.get_all('select * from students where age>60')
for row in res:
print(“%d--%d” %(row[0] row[1] … row[n]))
万水千山总是情,点个