【MySQL与python交互】06 数据库查询操作

"""
fetchone()
功能:获取下一个查询结果集,结果集是一个对象

fetchall()
功能:接收全部的返回的行

rowcount:是一个只读属性,返回excute()方法影响的行数

"""
import pymysql
db = pymysql.connect('本机ip', 'root', 'root', 'aimei')
# 创建一个cursor对象
cursor = db.cursor()

sql = 'select * from bandcard where money>400'
try:
    cursor.execute(sql)
    reslist = cursor.fetchall()
    for row in reslist:
        print('%d--%d'%(row[0],row[1]))
except:
    # 如果提交失败,回滚到上一次的数据
    db.rollback()
    print('操作失败!')

# 断开
cursor.close()
db.close()



"""
fetchone()
功能:获取下一个查询结果集,结果集是一个对象

fetchall()
功能:接收全部的返回的行

rowcount:是一个只读属性,返回excute()方法影响的行数

"""
import pymysql
db = pymysql.connect('192.168.0.105', 'root', 'root', 'aimei')
# 创建一个cursor对象
cursor = db.cursor()

sql = 'select * from bandcard where money>400'
try:
    cursor.execute(sql)
    reslist = cursor.fetchall()
    for row in reslist:
        print('%d--%d'%(row[0],row[1]))
except:
    # 如果提交失败,回滚到上一次的数据
    db.rollback()
    print('操作失败!')

# 断开
cursor.close()
db.close()



发布了112 篇原创文章 · 获赞 114 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_38114487/article/details/105479600