溫馨提示×

溫馨提示×

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

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

怎么在Python中使用fetchone()查詢mysql數(shù)據(jù)庫

發(fā)布時(shí)間:2021-05-11 15:49:55 來源:億速云 閱讀:287 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章給大家介紹怎么在Python中使用fetchone()查詢mysql數(shù)據(jù)庫,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

python主要應(yīng)用領(lǐng)域有哪些

1、云計(jì)算,典型應(yīng)用OpenStack。2、WEB前端開發(fā),眾多大型網(wǎng)站均為Python開發(fā)。3.人工智能應(yīng)用,基于大數(shù)據(jù)分析和深度學(xué)習(xí)而發(fā)展出來的人工智能本質(zhì)上已經(jīng)無法離開python。4、系統(tǒng)運(yùn)維工程項(xiàng)目,自動(dòng)化運(yùn)維的標(biāo)配就是python+Django/flask。5、金融理財(cái)分析,量化交易,金融分析。6、大數(shù)據(jù)分析。

demo.py(查詢,取出一條數(shù)據(jù),fetchone):

from pymysql import *
def main():
  # 創(chuàng)建Connection連接
  conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8')
  # 獲得Cursor對象
  cs1 = conn.cursor()
  # 執(zhí)行select語句,并返回受影響的行數(shù):查詢一條數(shù)據(jù)
  count = cs1.execute('select id,name from goods where id>=4')
  # 打印受影響的行數(shù)
  print("查詢到%d條數(shù)據(jù):" % count)
  for i in range(count):
    # 獲取查詢的結(jié)果
    result = cs1.fetchone()
    # 打印查詢的結(jié)果
    print(result) # 元組 (1, '張三', 20, '男')
    # 獲取查詢的結(jié)果
  # 關(guān)閉Cursor對象
  cs1.close()
  conn.close()
if __name__ == '__main__':
  main()

demo.py(查詢,取出多條數(shù)據(jù),fetchmany,fetchall):

from pymysql import *
def main():
  # 創(chuàng)建Connection連接
  conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8')
  # 獲得Cursor對象
  cs1 = conn.cursor()
  # 執(zhí)行select語句,并返回受影響的行數(shù):查詢一條數(shù)據(jù)
  count = cs1.execute('select id,name from goods where id>=4')
  # 打印受影響的行數(shù)
  print("查詢到%d條數(shù)據(jù):" % count)
  # for i in range(count):
  #   # 獲取查詢的結(jié)果
  #   result = cs1.fetchone()  # 取出一條記錄,返回元組。
  #   # 打印查詢的結(jié)果
  #   print(result)
  #   # 獲取查詢的結(jié)果
  # 獲取所有記錄
  result = cs1.fetchall() # fetchmany(3) 取出3條記錄,返回二維元組。
  print(result)  # 二維元組
  # 關(guān)閉Cursor對象
  cs1.close()
  conn.close()
if __name__ == '__main__':
  main()

關(guān)于怎么在Python中使用fetchone()查詢mysql數(shù)據(jù)庫就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

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

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

AI