python操作数据库
调用fetchone游标一次,就会往下执行一句
fetchmany 获取多行
fetchall 获取全部,运行时候需要占用大量内存(如果数据库中数据比较多的话)
host = ''
user = ''
passwd = ''
dbname = ''
import pymysql
conn = pymysql.connect(host=host,
user=user,
passwd=passwd,
db=dbname,
charset='utf8')
c=conn.cursor()
sql = 'SELECT * FROM SQ_COURSE'
c.execute()
# 获取行数据
# fetchone、fetchmany
numrows = c.rowcount
print("we have %s rows"%numrows)
for one in range(numrows):
row=c.fetchone()
print(row)
# 检查获取回来的记录中有没有xxx
crouseName =row[1]
if crouseName == 'python3'
pythonfound =True
if pythonfound:
print("测试通过,找到课程")
else:
print("测试不通过,没有找到课程")
插入数据:执行语句之后一定要有commit,跟查询不同