亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python3.6簡(jiǎn)單的操作Mysql數(shù)據(jù)庫(kù)的三個(gè)實(shí)例

 更新時(shí)間:2018年10月17日 14:19:52   作者:no-96  
今天小編就為大家分享一篇關(guān)于Python3.6簡(jiǎn)單的操作Mysql數(shù)據(jù)庫(kù)的三個(gè)實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧

安裝pymysql

參考:https://github.com/PyMySQL/PyMySQL/

pip install pymsql

實(shí)例一

import pymysql
# 創(chuàng)建連接
# 參數(shù)依次對(duì)應(yīng)服務(wù)器地址,用戶(hù)名,密碼,數(shù)據(jù)庫(kù)
conn = pymysql.connect(host='127.0.0.1', user='root', passwd='123456', db='demo')
# 創(chuàng)建游標(biāo)
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 執(zhí)行語(yǔ)句返回影響的行數(shù)
effect_row = cursor.execute("select * from course")
print(effect_row)
# 獲取所有數(shù)據(jù)
result = cursor.fetchall()
result = cursor.fetchone() # 獲取下一個(gè)數(shù)據(jù)
result = cursor.fetchone() # 獲取下一個(gè)數(shù)據(jù)(在上一個(gè)的基礎(chǔ)之上)
# cursor.scroll(-1, mode='relative') # 相對(duì)位置移動(dòng)
# cursor.scroll(0,mode='absolute') # 絕對(duì)位置移動(dòng)
# 提交,不然無(wú)法保存新建或者修改的數(shù)據(jù)
conn.commit()
# 關(guān)閉游標(biāo)
cursor.close()
# 關(guān)閉連接
conn.close()

實(shí)例二

import pymysql
# 建立連接
conn = pymysql.connect(host='127.0.0.1', user='root', passwd='123456', db='demo')
# 創(chuàng)建游標(biāo)
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 插入一條數(shù)據(jù) %s是占位符 占位符之間用逗號(hào)隔開(kāi)
effect_row = cursor.execute("insert into course(cou_name,time) values(%s,%s)", ("Engilsh", 100))
print(effect_row)
conn.commit()
cursor.close()
conn.close()

實(shí)例三

import pymysql.cursors
# Connect to the database
connection = pymysql.connect(host='localhost',
        user='user',
        password='passwd',
        db='db',
        charset='utf8mb4',
        cursorclass=pymysql.cursors.DictCursor)
try:
 with connection.cursor() as cursor:
  # Create a new record
  sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
  cursor.execute(sql, ('webmaster@python.org', 'very-secret'))
 # connection is not autocommit by default. So you must commit to save
 # your changes.
 connection.commit()
 with connection.cursor() as cursor:
  # Read a single record
  sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
  cursor.execute(sql, ('webmaster@python.org',))
  result = cursor.fetchone()
  print(result)
finally:
 connection.close()

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

相關(guān)文章

最新評(píng)論