没有合适的资源?快使用搜索试试~ 我知道了~
首页pymysql之cur.fetchall() 和cur.fetchone()用法详解
我就废话不多说了,大家还是直接看代码吧! import pymysql,hashlib 结果:单条结果 {'id': 1, 'name': '打车', 'phone': '132453'} sql = 'select * from zxj' def op_mysql(sql,many=True): db_info = {'user': 'jxz', 'password': '123456', 'host': '118*******', 'db': 'jxz', 'port': 3306, 'charset': 'utf8', 'autocommit': True} conn =
资源详情
资源评论
资源推荐

pymysql之之cur.fetchall() 和和cur.fetchone()用法详解用法详解
我就废话不多说了,大家还是直接看代码吧!
import pymysql,hashlib
结果:单条结果 {'id': 1, 'name': '打车', 'phone': '132453'}
sql = 'select * from zxj'
def op_mysql(sql,many=True):
db_info = {'user': 'jxz', 'password': '123456',
'host': '118*******', 'db': 'jxz', 'port': 3306, 'charset': 'utf8',
'autocommit': True}
conn = pymysql.connect(**db_info) # 建立连接
cur = conn.cursor(pymysql.cursors.DictCursor) # 游标
cur.execute(sql) # 执行sql语句,insert 、update 、delete
if many:
result = cur.fetchall()
print('多条',result)
else:
result = cur.fetchone() # {''}
print('dantiao',result)
cur.close()
conn.close()
return result
op_mysql(sql,many=False) # 传入sql, 默认为true
=================================
md5加盐2
def md5(s,salt=''):
new_s = str(s) + salt
m = hashlib.md5(new_s.encode())
return m.hexdigest()```
补充知识:补充知识:python pymssql使用时,使用使用时,使用fetchone获取的值怎么在获取的值怎么在while里操作多条数据里操作多条数据
项目描述:
想把status状态为1的数据查出来然后再通过while 遍历update 数据,为了清楚测试时候的数据。
刚开始的代码是这样的。
#coding:utf-8
import pymssql
def connect():
connect=pymssql.connect((‘x.x.x.x'),‘x',‘x',‘x')
cursor = connect.cursor() # 创建游标
sql001='select *from xxxxx where xxxxx=273and Status=1 order by sysno desc'#查询语句
cursor.execute(sql001)
row=cursor.fetchone()#读取查询结果
print(row)
if row==None:
print("没有查到数据")
else:
while row:
print("sysno=%s" % (row[0]))
cursor.execute("update xxxxx set Status=-1 where SysNo=%d", row[0]) # 执行语句\
connect.commit()
print(row)
#cursor.execute(sql001)
row=cursor.fetchone()
#print(row)
connect()
报错信息:
File “D:/JiCaiZhuanTi/Case/test.py”, line 22, in connect



















安全验证
文档复制为VIP权益,开通VIP直接复制

评论0