Python使用总结
提示:数据库篇
pymsql的数据库常用操作
文章目录
提示:以下是本篇文章正文内容,下面案例可供参考
一、pymysql的连接
数据库的连接
使用db将连接数据库端,获取数据库的数据,
user一般是root
password是密码,默认是123456
database是数据库
db = pymysql.connect(host="localhost",port=3306, user="root", password="1234", database="weixiu_2", charset='utf8' )
获取数据游标
默认情况下,采用这种方式获取游标,默认数据库返回结果为元祖类型:
cursor = db.cursor()
这种方式获取游标,设置数据库返回结果为字典类型:
cursor = db.cursor(pymysql.cursors.DictCursor)
二、数据库的操作
1.select查询操作
sql语句就是正常的查询sql语句
cursor.execute(sql)就是去执行sql语句的操作
cds是获取数据库端返回的所有数据
db = pymysql.connect(host="localhost",port=3306, user="root", password="123456", database="weixiu_2", charset='utf8' )
cursor = db.cursor()
sql='''
SELECT t_scheme.scheme_id_, t_scheme.avg_time_, t_scheme.cost_
FROM t_scheme
ORDER BY t_scheme.avg_time_
'''
cursor.execute(sql)
cds=cursor.fetchall()
cds=list(cds)#转换为list类型
print(cds)
高级写法:带变量的写法,可以在sql这里设置自己想要的参数信息,本质上还是字符串的使用
写法一:format方式
sql语句可以后面接format,就可以实现在sql中插入变量的效果,就不用机械的写成数字1,0等等,将sql语句和变量联系起来
import pymysql
db = pymysql.connect(host="localhost", port=3306, user="root", password="123456", database="weixiu_2", charset='utf8')
cursor = db.cursor()
time=1
excel=1
sql='''
SELECT t_scheme.scheme_id_, t_scheme.avg_time_, t_scheme.cost_
FROM t_scheme
where t_scheme.sc_type={0} AND t_scheme.evaluate={1}
'''.format(time,excel)
cursor.execute(sql)
cds=cursor.fetchall()
print(list(cds))
写法二:%s形式
sql中用%s的形式表示未知数,可以是字符型,可以是数字型,统一用%s表示
data中记录这对应%s的数据,记住顺序是固定的!
在cursor.execute中提交两个参数,sql和data
import pymysql
db = pymysql.connect(host="localhost", port=3306, user="root", password="123456", database="weixiu_2", charset='utf8')
cursor = db.cursor()
sql='''
SELECT t_scheme.scheme_id_, t_scheme.avg_time_, t_scheme.cost_
FROM t_scheme
where t_scheme.scheme_id_=%s AND t_scheme.evaluate=%s
'''
data=['S00020',1]
cursor.execute(sql,data)
cds=cursor.fetchall()
print(list(cds))
2.update更新操作
update更新操作,sql语句基本结构:update 表名 set 某个属性=啥啥 where 限定条件
注意,最后操作是db.commit()表示提交操作
同理可以采用%s形式
import pymysql
db = pymysql.connect(host="localhost", port=3306, user="root", password="123456", database="weixiu_2", charset='utf8')
cursor = db.cursor()
sql="update t_scheme set t_scheme.evaluate=2 where t_scheme.scheme_id_='{}'"
.format(i[0])
cursor.execute(sql)
db.commit()
3.insert更新操作
insert into 表名(需要添加的列的名字)
values(对应列所需要的值)
val与values中的符号对应
后面的操作与update一样
import pymysql
db = pymysql.connect(host="localhost", port=3306, user="root", password="123456", database="weixiu_2", charset='utf8')
cursor = db.cursor()
sql='''
insert into t_s_ba (ba_id_,scheme_id_,b_id_,a_id_,)
VALUES ( %s, %s, %s, %s)
'''
val=(rd.SBA[0],rd.SBA[1],rd.SBA[2],rd.SBA[3])
# 执行sql语句
cursor.execute(sql,val)
# 提交到数据库执行
db.commit()
高级写法:批量输入数据
val可以是一个元祖的元祖,表示批量存储信息
cursor.executemany() 表示提交多条数据,针对于val
import pymysql
db = pymysql.connect(host="localhost", port=3306, user="root", password="123456", database="weixiu_2", charset='utf8')
cursor = db.cursor()
sql='''
insert into t_s_ba (ba_id_,scheme_id_,b_id_,a_id_,)
VALUES ( %s, %s, %s, %s)
'''
val=((rd.SBA[1][0],rd.SBA[1][1],rd.SBA[1][2],rd.SBA[1][3]),
(rd.SBA[2][0],rd.SBA[2][1],rd.SBA[2][2],rd.SBA[2][3]),
(rd.SBA[3][0],rd.SBA[3][1],rd.SBA[3][2],rd.SBA[3][3]))
# 执行sql语句
cursor.executemany(sql,val)
# 提交到数据库执行
db.commit()
4.delete删除操作
和上面一样的操作,没有什么区别,也是%s和format的写法,这里就只写%s了
import pymysql
db = pymysql.connect(host="localhost", port=3306, user="root", password="123456", database="weixiu_2", charset='utf8')
cursor = db.cursor()
sql = 'delete from info where name=%s'
del_data=['2']
cursor.execute(sql,del_data)
db.commit()
5.最后操作:
在最后要关闭数据库,以及进行排错处理(可有可无)
except Exception as e:
print(e)
#数据回滚,仅用于删除操作
db.rollback()
finally:
db.close()