python教程

python31-python操作数据库

2019-06-05  本文已影响0人  筱媛媛

本篇文章主要讲的是python操作mysql数据库,包括数据库的连接以及数据库的增删改查,常用的方法包括connect、cursor、execute、commit、fetchone、fetchall、rowcount、close。希望感兴趣的小伙伴可以坚持看下去同时欢迎提出宝贵的意见让我们一起进步!

01:安装MySQLdb模块

1)概述:MySQLdb用于python连接Mysql数据库的接口。

2)命令:pip install mysqlclient

02:python连接数据库前提条件

①已经创建了数据库TESTDB

②连接数据库TESTDB使用的用户名和密码

③保证主机上已经安装MySQLdb模块

03:连接数据库
import MySQLdb
#打开数据库连接
db=MySQLdb.connect(host='127.0.0.1',user='root',passwd='123',db='mysql',charset='utf8')
#使用cursor()方法获得操作游标:返回一个cursor对象,中文叫游标
cursor=db.cursor()
#使用execute()方法执行sql语句
cursor.execute('select version()')
#使用fetchone()方法获取一条数据
data=cursor.fetchone()
print('当前数据库的版本:',data)
db.close()#关闭数据库连接
04:创建数据库表

sql=create table student(id varchar(50), name varchar(50) not null, age int,score int )

import MySQLdb
db=MySQLdb.connect(host='127.0.0.1',user='root',passwd='123',db='mysql',charset='utf8')
cursor=db.cursor()
#如果数据表已经存在使用execute()方法删除表
cursor.execute('drop table if exists student')
#构造建表sql语句
sql='''create table student(
        id  varchar(50),
        name varchar(50) not null,
        age int,
        score int )'''
print(cursor.execute(sql))
db.close()
05:数据库插入操作

1)所有对数据库修改的动作,必须要commit才会生效

2)加入try except原因:因为在真实场景当中,执行的sql语句可能会是多条,而数据之间存在外键关系或其他逻辑关系一旦某条sql报错而其他sql不报错(报错的sql没有插入数据进去,其他的 sql 没报错,成功插入了数据),就会造成大量脏数据,引发bug,带来大量的数据修复工作,所以我们用try捕获异常,一旦任意sql报错即刻回滚操作。

3)sql=insert into student(id,name,age,score)values('123d45e6','mike',25,60)

import MySQLdb,uuid
db=MySQLdb.connect(host='127.0.0.1',user='root',passwd='123',db='mysql',charset='utf8')
cursor=db.cursor()
studentId=uuid.uuid4()
#构造插入sql语句
sql='''insert into student(id,name,age,score)
        values('%s','mike',25,60) '''%(studentId)
try:
    #执行sql语句
    cursor.execute(sql)
    #提交到数据库执行
    db.commit()
    print('执行成功')
except:
    #插入数据有问题时需要回滚代码
    db.rollback()
print(sql)
db.close()
06:数据库更新操作

sql=update student set name='lisa' where id='98351c37-ff222cb-43ce-b691

import MySQLdb
db=MySQLdb.connect(host='127.0.0.1',user='root',passwd='123',db='mysql',charset='utf8')
cursor=db.cursor()
sql="update student set name='lisa' where id='98351c37-ff222cb-43ce-b691-a80674354285'"
try:
    #执行sql语句
    cursor.execute(sql)
    #提交到数据库执行
    db.commit()
    print('执行成功')
except:
    #更新数据有问题时需要回滚代码
    db.rollback()
    print('回滚成功')
print(sql)
db.close()
07:数据库查询操作

1)Python查询 Mysql使用fetchone()方法获取单条数据, 使用 fetchall()方法获取多条数据

①etchone():该方法获取下一个查询结果集。结果集是一个对象

②fetchall():接收全部返回结果

rowcount:这是一个只读属性,并返回执行execute方法后影响的行数

2)sql=select * from student

import MySQLdb
db=MySQLdb.connect(host='127.0.0.1',user='root',passwd='123',db='mysql',charset='utf8')
cursor=db.cursor()
#构建sql查询语句
sql="select * from student"
try:
    cursor.execute(sql)
    #获取查询结果
    # data=cursor.fetchone()#获取一个
    data=cursor.rowcount#受影响的行数
    print(data)
    data=cursor.fetchall()#获取全部
    for row in data:
        print(row)
except:
    print('查询失败')
db.close()
08:数据库删除操作

sql=delete from student where age=30

import MySQLdb
db=MySQLdb.connect(host='127.0.0.1',user='root',passwd='123',db='mysql',charset='utf8')
cursor=db.cursor()
#构建sql删除语句
sql="delete from student where age=30"
try:
    #执行sql语句
    cursor.execute(sql)
    #提交到数据库执行
    db.commit()
    print('执行成功')
except:
    #数据删除错误时需要回滚代码
    db.rollback()
    print('回滚成功')
print(sql)
db.close()
上一篇 下一篇

猜你喜欢

热点阅读