最新国产好看的视频,伊人天堂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)論

南丰县| 建湖县| 大新县| 广灵县| 都匀市| 泽州县| 北海市| 张家口市| 金坛市| 确山县| 依安县| 嘉义市| 郯城县| 邳州市| 宁强县| 东港市| 定安县| 滨海县| 会泽县| 朔州市| 青阳县| 湛江市| 龙胜| 怀宁县| 深圳市| 霍山县| 张家界市| 南靖县| 临海市| 济南市| 颍上县| 民县| 黄陵县| 玉龙| 邹平县| 广宁县| 卢氏县| 即墨市| 沂源县| 武穴市| 淳安县|