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

關于pymysql模塊的使用以及代碼詳解

 更新時間:2019年09月01日 14:06:32   作者:三國小夢  
在本篇文章里小編給大家整理的是關于關于pymysql模塊的使用以及代碼詳解,有興趣的朋友們學習下。

pymysql模塊的使用

查詢一條數(shù)據(jù)fetchone()

from pymysql import *

conn = connect(
  host='127.0.0.1',
  port=3306, user='root',
  password='123456',
  database='itcast',
  charset='utf8')

# 創(chuàng)建游標
c = conn.cursor()
# 執(zhí)行sql語句
c.execute("select * from student")
# 查詢一行數(shù)據(jù)
result = c.fetchone()
print(result)
# 關閉游標
c.close()
# 關閉數(shù)據(jù)庫連接
conn.close()
"""
(1, '張三', 18, b'\x01')
"""

查詢多條數(shù)據(jù)fetchall()

from pymysql import *

conn = connect(
  host='127.0.0.1',
  port=3306, user='root',
  password='123456',
  database='itcast',
  charset='utf8')

# 創(chuàng)建游標
c = conn.cursor()
# 執(zhí)行sql語句
c.execute("select * from student")
# 查詢多行數(shù)據(jù)
result = c.fetchall()
for item in result:
  print(item)
# 關閉游標
c.close()
# 關閉數(shù)據(jù)庫連接
conn.close()
"""
(1, '張三', 18, b'\x01')
(2, '李四', 19, b'\x00')
(3, '王五', 20, b'\x01')
"""

更改游標的默認設置,返回值為字典

from pymysql import *

conn = connect(
  host='127.0.0.1',
  port=3306, user='root',
  password='123456',
  database='itcast',
  charset='utf8')

# 創(chuàng)建游標,操作設置為字典類型
c = conn.cursor(cursors.DictCursor)
# 執(zhí)行sql語句
c.execute("select * from student")
# 查詢多行數(shù)據(jù)
result = c.fetchall()
for item in result:
  print(item)
# 關閉游標
c.close()
# 關閉數(shù)據(jù)庫連接
conn.close()
"""
{'id': 1, 'name': '張三', 'age': 18, 'sex': b'\x01'}
{'id': 2, 'name': '李四', 'age': 19, 'sex': b'\x00'}
{'id': 3, 'name': '王五', 'age': 20, 'sex': b'\x01'}
"""

返回一條數(shù)據(jù)時也是一樣的。返回字典或者時元組看個人需要。

2|2使用數(shù)據(jù)操作語句

執(zhí)行增加、刪除、更新語句的操作其實是一樣的。只寫一個作為示范。

from pymysql import *

conn = connect(
  host='127.0.0.1',
  port=3306, user='root',
  password='123456',
  database='itcast',
  charset='utf8')

# 創(chuàng)建游標
c = conn.cursor()
# 執(zhí)行sql語句
c.execute("insert into student(name,age,sex) values (%s,%s,%s)",("小二",28,1))
# 提交事務
conn.commit()
# 關閉游標
c.close()
# 關閉數(shù)據(jù)庫連接
conn.close()

和查詢語句不同的是必須使用commit()提交事務,否則操作就是無效的。

3|0編寫數(shù)據(jù)庫連接類

普通版

MysqlHelper.py

from pymysql import connect,cursors

class MysqlHelper:
  def __init__(self,
         host="127.0.0.1",
         user="root",
         password="123456",
         database="itcast",
         charset='utf8',
         port=3306):
    self.host = host
    self.port = port
    self.user = user
    self.password = password
    self.database = database
    self.charset = charset
    self._conn = None
    self._cursor = None

  def _open(self):
    # print("連接已打開")
    self._conn = connect(host=self.host,
               port=self.port,
               user=self.user,
               password=self.password,
               database=self.database,
               charset=self.charset)
    self._cursor = self._conn.cursor(cursors.DictCursor)

  def _close(self):
    # print("連接已關閉")
    self._cursor.close()
    self._conn.close()

  def one(self, sql, params=None):
    result: tuple = None
    try:
      self._open()
      self._cursor.execute(sql, params)
      result = self._cursor.fetchone()
    except Exception as e:
      print(e)
    finally:
      self._close()
    return result

  def all(self, sql, params=None):
    result: tuple = None
    try:
      self._open()
      self._cursor.execute(sql, params)
      result = self._cursor.fetchall()
    except Exception as e:
      print(e)
    finally:
      self._close()
    return result

  def exe(self, sql, params=None):
    try:
      self._open()
      self._cursor.execute(sql, params)
      self._conn.commit()
    except Exception as e:
      print(e)
    finally:
      self._close()

該類封裝了fetchone、fetchall、execute,省去了數(shù)據(jù)庫連接的打開和關閉和游標的打開和關閉。

下面的代碼是調用該類的小示例:

from MysqlHelper import *

mysqlhelper = MysqlHelper()
ret = mysqlhelper.all("select * from student")
for item in ret:
  print(item)
"""
{'id': 1, 'name': '張三', 'age': 18, 'sex': b'\x01'}
{'id': 2, 'name': '李四', 'age': 19, 'sex': b'\x00'}
{'id': 3, 'name': '王五', 'age': 20, 'sex': b'\x01'}
{'id': 5, 'name': '小二', 'age': 28, 'sex': b'\x01'}
{'id': 6, 'name': '娃哈哈', 'age': 28, 'sex': b'\x01'}
{'id': 7, 'name': '娃哈哈', 'age': 28, 'sex': b'\x01'}
"""

上下文管理器版

mysql_with.py

from pymysql import connect, cursors

class DB:
  def __init__(self,
         host='localhost',
         port=3306,
         db='itcast',
         user='root',
         passwd='123456',
         charset='utf8'):
    # 建立連接
    self.conn = connect(
      host=host,
      port=port,
      db=db,
      user=user,
      passwd=passwd,
      charset=charset)
    # 創(chuàng)建游標,操作設置為字典類型
    self.cur = self.conn.cursor(cursor=cursors.DictCursor)

  def __enter__(self):
    # 返回游標
    return self.cur

  def __exit__(self, exc_type, exc_val, exc_tb):
    # 提交數(shù)據(jù)庫并執(zhí)行
    self.conn.commit()
    # 關閉游標
    self.cur.close()
    # 關閉數(shù)據(jù)庫連接
    self.conn.close()

如何使用:

from mysql_with import DB

with DB() as db:
  db.execute("select * from student")
  ret = db.fetchone()
  print(ret)

"""
{'id': 1, 'name': '張三', 'age': 18, 'sex': b'\x01'}
"""

以上就是本次介紹的全部知識點內容,感謝大家的閱讀和對腳本之家的支持。

相關文章

  • Python中的變量及簡單數(shù)據(jù)類型應用

    Python中的變量及簡單數(shù)據(jù)類型應用

    這篇文章主要介紹了Python中的變量及簡單數(shù)據(jù)類型應用,簡單的數(shù)據(jù)類型包括字符串和數(shù)字,更多詳細內容,需要的小伙伴可以參考一下
    2022-03-03
  • Python新手學習裝飾器

    Python新手學習裝飾器

    在本篇文章里小編給大家整理的是一篇關于Python裝飾器的相關知識點內容,需要的朋友們可以學習下。
    2020-06-06
  • pyenv虛擬環(huán)境管理python多版本和軟件庫的方法

    pyenv虛擬環(huán)境管理python多版本和軟件庫的方法

    這篇文章主要介紹了pyenv虛擬環(huán)境管理python多版本和軟件庫,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-12-12
  • selenium獲取當前頁面的url、源碼、title的方法

    selenium獲取當前頁面的url、源碼、title的方法

    這篇文章主要介紹了selenium獲取當前頁面的url、源碼、title的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-06-06
  • 詳解使用python3.7配置開發(fā)釘釘群自定義機器人(2020年新版攻略)

    詳解使用python3.7配置開發(fā)釘釘群自定義機器人(2020年新版攻略)

    這篇文章主要介紹了詳解使用python3.7配置開發(fā)釘釘群自定義機器人(2020年新版攻略),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-04-04
  • 詳解Python如何利用Shelve進行數(shù)據(jù)存儲

    詳解Python如何利用Shelve進行數(shù)據(jù)存儲

    Shelve是Python標準庫中的一個模塊,用于實現(xiàn)簡單的數(shù)據(jù)持久化,本文將詳細介紹Shelve模塊的功能和用法,并提供豐富的示例代碼,希望對大家有所幫助
    2023-11-11
  • python文件的讀取、寫入與刪除

    python文件的讀取、寫入與刪除

    文件是無處不在的,,無論我們使用哪種編程語言,處理文件對于每個程序員都是必不可少的,下面這篇文章主要給大家介紹了關于python文件的讀取、寫入與刪除的相關資料,文中通過實例代碼和圖文介紹的非常詳細,需要的朋友可以參考下
    2023-06-06
  • Django利用elasticsearch(搜索引擎)實現(xiàn)搜索功能

    Django利用elasticsearch(搜索引擎)實現(xiàn)搜索功能

    這篇文章主要介紹了Django利用elasticsearch(搜索引擎)實現(xiàn)搜索功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • python中的函數(shù)用法入門教程

    python中的函數(shù)用法入門教程

    這篇文章主要介紹了python中的函數(shù)用法,包括了函數(shù)的定義及參數(shù)的各種注意事項等,對Python初學者有很好的借鑒價值,需要的朋友可以參考下
    2014-09-09
  • 對python讀寫文件去重、RE、set的使用詳解

    對python讀寫文件去重、RE、set的使用詳解

    今天小編就為大家分享一篇對python讀寫文件去重、RE、set的使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12

最新評論

高邮市| 金乡县| 宁阳县| 集安市| 镇宁| 肃南| 南澳县| 深圳市| 安岳县| 铁岭县| 阳新县| 兴文县| 巨鹿县| 岑巩县| 东方市| 柳州市| 青浦区| 津市市| 兴义市| 长治市| 丽水市| 蒙城县| 景德镇市| 城口县| 名山县| 东源县| 宕昌县| 乌拉特前旗| 成武县| 盐边县| 鲜城| 临沭县| 秦皇岛市| 富锦市| 哈巴河县| 阜新| 绍兴县| 卓尼县| 罗源县| 巨鹿县| 宣城市|