最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python3.6簡(jiǎn)單操作Mysql數(shù)據(jù)庫(kù)

 更新時(shí)間:2017年09月12日 09:51:59   作者:方程同調(diào)士  
這篇文章主要為大家詳細(xì)介紹了Python3.6簡(jiǎn)單操作Mysql數(shù)據(jù)庫(kù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文為大家分享了Python3.6操作Mysql數(shù)據(jù)庫(kù)的具體實(shí)例,供大家參考,具體內(nèi)容如下

安裝pymysql

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

pip install pymsql

實(shí)例一

import pymysql

# 創(chuàng)建連接
# 參數(shù)依次對(duì)應(yīng)服務(wù)器地址,用戶名,密碼,數(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)隔開
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()

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

瑞昌市| 弋阳县| 五家渠市| 大理市| 日土县| 阿拉善右旗| 久治县| 高密市| 乌拉特后旗| 阳信县| 深泽县| 南丹县| 山丹县| 安丘市| 公主岭市| 栾川县| 托克逊县| 贵溪市| 琼中| 汝城县| 南涧| 白沙| 军事| 扎赉特旗| 巨鹿县| 崇左市| 宜兰市| 东兴市| 吴忠市| 临武县| 灵丘县| 徐州市| 林甸县| 张家港市| 阿尔山市| 子洲县| 北碚区| 新民市| 玉门市| 德化县| 石林|