溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

pymysql怎么使用cur.fetchall()和cur.fetchone()

發(fā)布時(shí)間:2020-07-27 10:20:47 來(lái)源:億速云 閱讀:275 作者:小豬 欄目:開發(fā)技術(shù)

這篇文章主要為大家展示了pymysql怎么使用cur.fetchall()和cur.fetchone(),內(nèi)容簡(jiǎn)而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會(huì)有收獲的,下面讓小編帶大家一起來(lái)看看吧。

我就廢話不多說(shuō)了,大家還是直接看代碼吧!

import pymysql,hashlib
結(jié)果:?jiǎn)螚l結(jié)果 {'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) # 游標(biāo)
 cur.execute(sql) # 執(zhí)行sql語(yǔ)句,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, 默認(rèn)為true

=================================
md5加鹽2
def md5(s,salt=''):
 new_s = str(s) + salt
 m = hashlib.md5(new_s.encode())
 return m.hexdigest()```

補(bǔ)充知識(shí):python pymssql使用時(shí),使用fetchone獲取的值怎么在while里操作多條數(shù)據(jù)

項(xiàng)目描述:

想把status狀態(tài)為1的數(shù)據(jù)查出來(lái)然后再通過(guò)while 遍歷update 數(shù)據(jù),為了清楚測(cè)試時(shí)候的數(shù)據(jù)。

剛開始的代碼是這樣的。

#coding:utf-8
import pymssql
def connect():
connect=pymssql.connect((‘x.x.x.x'),‘x',‘x',‘x')

cursor = connect.cursor() # 創(chuàng)建游標(biāo)
sql001='select *from xxxxx where xxxxx=273and Status=1 order by sysno desc'#查詢語(yǔ)句
cursor.execute(sql001)
row=cursor.fetchone()#讀取查詢結(jié)果
print(row)
if row==None:
 print("沒(méi)有查到數(shù)據(jù)")
else:

 while row:
 print("sysno=%s" % (row[0]))
 cursor.execute("update xxxxx set Status=-1 where SysNo=%d", row[0]) # 執(zhí)行語(yǔ)句\
 connect.commit()
 print(row)
 #cursor.execute(sql001)
 row=cursor.fetchone()
 #print(row)

connect()

報(bào)錯(cuò)信息:

File “D:/JiCaiZhuanTi/Case/test.py”, line 22, in connect
row=cursor.fetchone()
File “src\pymssql.pyx”, line 507, in pymssql.Cursor.fetchone
pymssql.OperationalError: Statement not executed or executed statement has no resultset

自己查了不少文章,以前沒(méi)有對(duì)這塊有所涉及,因?yàn)楸救耸遣锁B,用到哪就看到哪。也仔細(xì)看了fetchone() 、fetchall() 還有pymssql的對(duì)數(shù)據(jù)庫(kù)的基本炒作??戳撕镁迷谧詈箪`光一閃理解錯(cuò)誤在哪里了。

錯(cuò)誤出在while里的connect.commit()后直接又row=cursor.fetchone()而while里是(返回單個(gè)的元組,也就是一條記錄(row),如果沒(méi)有結(jié)果 則返回 None)因?yàn)槲疑弦粋€(gè)查詢是update語(yǔ)句,更新sql語(yǔ)句不會(huì)返回resultset,所以會(huì)報(bào)錯(cuò)。

然后我就這樣改了一下,:

while row:
print(“sysno=%s” % (row[0]))
cursor.execute(“update xxxxx set Status=-1 where SysNo=%d”, row[0]) # 執(zhí)行語(yǔ)句
connect.commit()
print(row)
cursor.execute(sql001)
row=cursor.fetchone()

在獲取sql執(zhí)行獲取結(jié)果的 row=cursor.fetchone()我再去調(diào)用一次查詢?cè)俅潍@取想要的數(shù)據(jù)。

我覺得應(yīng)該有更好的辦法,就是再第一次獲取查詢結(jié)果把所需要的sysno都拿出來(lái),然后再while,這樣可以減少對(duì)數(shù)據(jù)庫(kù)的調(diào)用。

以上就是關(guān)于pymysql怎么使用cur.fetchall()和cur.fetchone()的內(nèi)容,如果你們有學(xué)習(xí)到知識(shí)或者技能,可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI