Python操作数据库
1、Python-MySQL
Python3连接MySQL主要有两种方式:MySQL-Connector模块和PyMySQL模块
主要区别如下:
-
MySQL-Connector:Oracle官方支持、略慢、不兼容MySQLdb
-
PyMySQL: 比MySQL-Connector略快,兼容MySQLdb
1.1、MySQL-Connector
安装:
pip install mysql-connector
使用注意:
# 若MySQL是8.x版本,密码插件验证方式发生了变化,需要做如下改变(否则报错):
# 1)修改my.ini配置:
default_authentication_plugin=caching_sha2_password
# 2)修改密码:
alter user 'root'@'localhost' identified with caching_sha2_password by 'password';
使用案例:
import mysql.connector
# 使用步骤:
# 1)创建连接
cn = mysql.connector.connect(
host='localhost',
port=3306,
user='root',
password='123456',
database='py_mysql'
)
# 2)创建游标对象:游标对象可以执行SQL语句并获取执行结果
cursor = cn.cursor()
# 3)执行SQL语句:cursor.execute('sql', [args])
# 4)提交更改:执行更新操作(插入、更新或删除)后,需要使用连接对象的commit()方法来提交更改,若发生错误,可使用rollback()方法进行事务回滚
# 5)获取执行结果:执行查询操作后,可以使用游标对象的fetchall()方法获取查询结果
# 6)关闭游标和连接,释放资源:cursor.close()、cn.close()
# A、创建数据库、创建表
cursor.execute("create table stu(name varchar(20), age int)")
# B、CRUD
# 1)增
# a、插入一条
sql = "insert into stu (name, age) values (%s, %s)"
val = ('Tom', 18)
cursor.execute(sql, val)
cn.commit()
# b、批量插入:executemany()
vals = [('Bob', 18), ('Jerry', 18)]
cursor.executemany(sql, vals)
cn.commit()
print(cursor.rowcount) # 返回影响行数
# 2)改
sql = "update stu set age=%s where name=%s"
val = (19, 'Bob')
cursor.execute(sql, val)
cn.commit()
# 3)删
sql = "delete from stu where name=%s"
val = ('Tom',)
cursor.execute(sql, val)
cn.commit()
# 4)查
sql = "select * from stu where name !='' order by age desc limit 3"
cursor.execute(sql)
print(cursor.fetchone()) # 获取下一个结果
result = cursor.fetchall() # 获取所有结果
for row in result:
print(row) # 以元组形式显示一行数据
# 释放资源
cursor.close()
cn.close()
1.2、PyMySQL
安装:
pip install PyMySQL
使用案例:
import pymysql
# 使用步骤:
# 1)创建连接
cn = pymysql.connect(
host='localhost'