欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python數(shù)據(jù)庫(kù)封裝實(shí)現(xiàn)代碼示例解析

 更新時(shí)間:2020年09月05日 10:09:31   作者:Python探索牛  
這篇文章主要介紹了Python數(shù)據(jù)庫(kù)封裝實(shí)現(xiàn)代碼示例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

Django中(原生mysql封裝)

1.函數(shù)封裝

import pymysql

# 查  所數(shù)據(jù)
def get_all(sql):
  conn = pymysql.connect(host="localhost", user="root", password="root", database="db6")
  cur = conn.cursor(cursor=pymysql.cursors.DictCursor)
  cur.execute(sql)
  res = cur.fetchall()
  cur.close()
  conn.close()
  return res


# 查 一行數(shù)據(jù)
def get_one(sql,args):
  conn = pymysql.connect(host="localhost", user="root", password="root", database="db6")
  cur = conn.cursor(cursor=pymysql.cursors.DictCursor)
  cur.execute(sql,args)
  res = cur.fetchone()
  cur.close()
  conn.close()
  return res



# 增 刪 改 都要提交 commit
def get_mif(sql,args):
  conn = pymysql.connect(host="localhost", user="root", password="root", database="db6")
  cur = conn.cursor(cursor=pymysql.cursors.DictCursor)
  cur.execute(sql,args)
  conn.commit()
  cur.close()
  conn.close()


# 增 刪 改 都要提交 commit
# 添加并且?guī)Х祷刂?
def get_create(sql,args):
    conn = pymysql.connect(host="localhost", user="root", password="root", database="db6")
    cur = conn.cursor(cursor=pymysql.cursors.DictCursor)
    cur.execute(sql,args)
    conn.commit()
    cur.close()
    conn.close()
    return cur.lastrowid
    # python插入記錄后取得主鍵id的方法(cursor.lastrowid和conn.insert_id())



# 增 刪 改 都要提交 commit
# 批量加入 以元祖的形式傳參數(shù)  就是添加幾次次提交一次
def mul_mode(sql, args):
    conn = pymysql.connect(host="localhost", user="root", password="root", database="db6")
    cur = conn.cursor(cursor=pymysql.cursors.DictCursor)
    # self.cursor.executemany("insert into user (id,name) values (%s,%s)",[(1,"aaa"),(2,"bbb"),(3,"ccc")]) 傳參方式
    cur.executemany(sql, args)
    conn.commit()
    cur.close()
    conn.close()

2.對(duì)象封裝

import pymysql
# 注意 args 參數(shù)可以傳空值[]
class Mysqls(object):
  def __init__(self):
     # 讀取配置文件
    self.connect()

  def connect(self):
    self.conn = pymysql.connect(host="localhost", user="root", password="root", database="db6")  # 可以把主機(jī)連接等寫(xiě)入配置文件 等
    self.cursor=self.conn.cursor(cursor=pymysql.cursors.DictCursor)

  # 獲取所以數(shù)據(jù)
  def get_all(self,sql,args):
     self.cursor.execute(sql,args)
     res = self.cursor.fetchall()
     return res

  # 獲取一行數(shù)據(jù)
  def get_one(self,sql,args):
    self.cursor.execute(sql, args)
    res = self.cursor.fetchone()
    return res

  # 添加 就是添加一次提交多次
  def get_mode (self,sql,args):
    self.cursor.execute(sql, args)
    self.conn.commit()

  # 添加并且?guī)Х祷刂?
  def get_create(self,sql,args):
    self.cursor.execute(sql,args)
    self.conn.commit()
    return self.cursor.lastrowid
    # python插入記錄后取得主鍵id的方法(cursor.lastrowid和conn.insert_id())

   # 批量加入 以元祖的形式傳參數(shù)  就是添加一次提交一次
  def mul_mode(self, sql, args):
    # self.cursor.executemany("insert into user (id,name) values (%s,%s)",[(1,"aaa"),(2,"bbb"),(3,"ccc")]) 傳參方式
    self.cursor.executemany(sql, args)
    self.conn.commit()

  def get_close(self):
      self.cursor.close()
      self.conn.close()

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

相關(guān)文章

  • pytorch查看模型weight與grad方式

    pytorch查看模型weight與grad方式

    這篇文章主要介紹了pytorch查看模型weight與grad方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-06-06
  • Django model序列化為json的方法示例

    Django model序列化為json的方法示例

    這篇文章主要介紹了Django model序列化為json的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10
  • numpy如何按條件給元素賦值np.where、np.clip

    numpy如何按條件給元素賦值np.where、np.clip

    這篇文章主要介紹了numpy如何按條件給元素賦值np.where、np.clip問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Python實(shí)現(xiàn)的幾個(gè)常用排序算法實(shí)例

    Python實(shí)現(xiàn)的幾個(gè)常用排序算法實(shí)例

    這篇文章主要介紹了Python實(shí)現(xiàn)的幾個(gè)常用排序算法實(shí)例例如直接插入排序、直接選擇排序、冒泡排序、快速排序等,需要的朋友可以參考下
    2014-06-06
  • 一文講解如何查看python腳本所依賴(lài)三方包及其版本

    一文講解如何查看python腳本所依賴(lài)三方包及其版本

    Python因?yàn)榫哂谐嗟牡谌綆?kù)而被大家喜歡,下面這篇文章主要給大家介紹了關(guān)于如何查看python腳本所依賴(lài)三方包及其版本的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-03-03
  • Python如何限制輸入的數(shù)范圍

    Python如何限制輸入的數(shù)范圍

    在Python中,我們可以使用多種方法來(lái)限制用戶(hù)輸入的數(shù)值范圍,今天通過(guò)實(shí)例代碼給大家分享Python限制輸入的數(shù)范圍,感興趣的朋友一起看看吧
    2024-05-05
  • python使用ctypes調(diào)用第三方庫(kù)時(shí)出現(xiàn)undefined?symbol錯(cuò)誤詳解

    python使用ctypes調(diào)用第三方庫(kù)時(shí)出現(xiàn)undefined?symbol錯(cuò)誤詳解

    python中時(shí)間的庫(kù)有time和datetime,pandas也有提供相應(yīng)的時(shí)間處理函數(shù),下面這篇文章主要給大家介紹了關(guān)于python使用ctypes調(diào)用第三方庫(kù)時(shí)出現(xiàn)undefined?symbol錯(cuò)誤的相關(guān)資料,需要的朋友可以參考下
    2023-02-02
  • 詳細(xì)介紹Python的鴨子類(lèi)型

    詳細(xì)介紹Python的鴨子類(lèi)型

    相信python的開(kāi)發(fā)者對(duì)于python的鴨子類(lèi)型比較熟悉,鴨子類(lèi)型在維基百科中的準(zhǔn)確定義是‘是動(dòng)態(tài)類(lèi)型的一種風(fēng)格。在這種風(fēng)格中,一個(gè)對(duì)象有效的語(yǔ)義,不是由繼承自特定的類(lèi)或?qū)崿F(xiàn)特定的接口,而是由"當(dāng)前方法和屬性的集合"決定’。所以這篇文章給大家python的鴨子類(lèi)型。
    2016-09-09
  • 關(guān)于Python函數(shù)參數(shù)的進(jìn)階用法

    關(guān)于Python函數(shù)參數(shù)的進(jìn)階用法

    這篇文章主要給大家分享的是Python函數(shù)參數(shù)的進(jìn)階用法,Python函數(shù)的參數(shù)根據(jù)函數(shù) 在調(diào)用時(shí) 傳參的形式分為關(guān)鍵字參數(shù)和位置參數(shù),下面文章小編就來(lái)介紹相關(guān)資料,需要的朋友可以參考一下
    2021-10-10
  • Python pass詳細(xì)介紹及實(shí)例代碼

    Python pass詳細(xì)介紹及實(shí)例代碼

    這篇文章主要介紹了Python pass詳細(xì)介紹及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2016-11-11

最新評(píng)論