Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口。
Python DB-API使用流程:
引入 API 模块
获取与数据库的连接
执行SQL语句和存储过程
关闭数据库连接
文章目录
MySQLdb
MySQLdb 是用于Python链接Mysql数据库的接口,它实现了 Python 数据库 API 规范 V2.0,基于 MySQL C API 上建立的
Python中使用的模块
Python2:MySQLdb
Python3:pymysql
创建数据库及表
创建数据库:
Create database learning
创建数据库表:
create table student(id varchar(10) not null primary key,name varchar(20)not null,grade int )
修改数据库的访问权限
Mysql默认root用户只能本地登录,所以需要新创建一个用户
(1)修改root的登录限制
use mysql;
mysql>update user set host='%' where user='root';
mysql>flush privileges; -- 刷新MySQL的系统权限相关表;
(2)创建新用户
grant all privileges on *.* to 'redhat'@'%' identified by ‘redhat’ with grant option;
pymysql使用
导入pymysql模块
Import pymysql
连接到数据库
#导入模块
Import pymysql
#配置连接参数
conn=pymysql.connect(host='192.168.31.9',port=3306,user='redhat',passwd='redhat',db='learning')
#使用游标访问对象
cur=conn.cursor()
#执行数据库的操作
cur.execute("select*fromstudent")
#接受所有的结果并显示
For r in cur.fetchall():
print(r)
#关闭数据的指针对象
cur.close()
#关闭数据库连接
conn.close()
创建数据库表
Import pymysql
conn=pymysql.connect(host='192.168.31.9',port=3306,user='redhat',passwd='redhat',db='learning')
cn=conn.cursor()
sql='create table user(id varchar(10)not null primary key,name varchar(20) not null,grade int)'
cn.execute(sql)
cn.close()
conn.close()
数据库查询操作
fetchone():该方法获取下一个查询结果集。结果集是一个对象
fetchall():接收全部的返回结果行.
rowcount:这是一个只读属性,并返回执行execute()方法后影响的行数。
在python中存储方式是集合的方式
(1)fetchone()
importpymysql
conn=pymysql.connect(host='192.168.31.9',port=3306,user='redhat',passwd='redhat',db='learning')
cn=conn.cursor()
sql='select*fromstudent'
cn.execute(sql)
print(cn.fetchone())
cn.close()
conn.close()
(2)fetchall()
importpymysql
conn=pymysql.connect(host='192.168.31.9',port=3306,user='redhat',passwd='redhat',db='learning')
cn=conn.cursor()
sql='select*fromstudent'
cn.execute(sql)
print(cn.fetchall())
cn.close()
conn.close()
(3)rowcount
importpymysql
conn=pymysql.connect(host='192.168.31.9',port=3306,user='redhat',passwd='redhat',db='learning')
cn=conn.cursor()
sql='select*fromstudent'
cn.execute(sql)
print(cn.rowcount)
cn.close()
conn.close()
数据库插入操作
Import pymysql
conn=pymysql.connect(host='192.168.31.9',port=3306,user='redhat',passwd='redhat',db='learning')
cn=conn.cursor()
sql="insert into student values('201703','xixi',100)"
try:
cn.execute(sql)
conn.commit()
except:
conn.rollback()
print(cn.rowcount)
cn.close()
conn.close()
数据库的更新
Import pymysql
conn=pymysql.connect(host='192.168.31.9',port=3306,user='redhat',passwd='redhat',db='learning')
cn=conn.cursor()
sql="update student set grade = 99 where id='201703'"
try:
cn.execute(sql)
conn.commit()
except:
conn.rollback()
print(cn.rowcount)
cn.close()
conn.close()
数据库的删除
Import pymysql
conn=pymysql.connect(host='192.168.31.9',port=3306,user='redhat',passwd='redhat',db='learning')
cn=conn.cursor()
sql="delete from student where id='201702'"
try:
cn.execute(sql)
conn.commit()
except:
conn.rollback()
print(cn.rowcount)
cn.close()
conn.close()
执行事务
Python DB API 2.0 的事务提供了两个方法 commit 或 rollback。
commit:向数据库提交 //数据库连接
rollback:发生错误回滚 //数据库连接
commit()方法游标的所有更新操作,rollback()方法回滚当前游标的所有操作。每一个方法都开始了一个新的事务。
SQL语句中携带参数
(1)不携带参数
importpymysql
conn=pymysql.connect(host='192.168.31.9',port=3306,user='redhat',passwd='redhat',db='learning')
cn=conn.cursor()
sql="select*fromstudentwhereid=%s"%(201701)
cn.execute(sql)
print(cn.fetchall())
cn.close()
conn.close()
(2)携带参数
importpymysql
conn=pymysql.connect(host='192.168.31.9',port=3306,user='redhat',passwd='redhat',db='learning')
cn=conn.cursor()
id='201701'
sql="select*fromstudentwhereid=%s"
cn.execute(sql,id)
print(cn.fetchall())
cn.close()
conn.close()
(3)携带多个参数
importpymysql
conn=pymysql.connect(host='192.168.31.9',port=3306,user='redhat',passwd='redhat',db='learning')
cn=conn.cursor()
id='student'
sql="select*from"+id+"whereid='201701'"
print(sql)
cn.execute(sql)
print(cn.fetchall())
cn.close()
conn.close()