1.连接数据库查询
代码示例:
#coding=utf-8 #防止乱码
import pymysql
import json
#获取连接对象
conn = pymysql.connect(host="127.0.0.1",
user="root",
password="root",
database="ofilecabinet",
charset="utf8")
#获取游标对象
cursor = conn.cursor()
#执行方法sql
sql = "select *from dev_site where siteId='210824970809785198' AND siteType='1'"
cursor.execute(sql)
#获取结果并进行断言
#print(cursor.fetchone())
result = cursor.fetchone()
print(result)
#result_data=list(result)
#print(result[1])
#断言,列表中第2个值
assert "WH-暂存柜02" == result[1]
print(result[1])
#关闭游标对象
cursor.close()
#关闭连接对象
conn.close()
2.连接数据库工具封装
代码示例:
#导包 pymy