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í)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Python接口測(cè)試數(shù)據(jù)庫(kù)封裝實(shí)現(xiàn)原理
- Python3實(shí)現(xiàn)的Mysql數(shù)據(jù)庫(kù)操作封裝類(lèi)
- Python操作Oracle數(shù)據(jù)庫(kù)的簡(jiǎn)單方法和封裝類(lèi)實(shí)例
- Python Sql數(shù)據(jù)庫(kù)增刪改查操作簡(jiǎn)單封裝
- Python實(shí)現(xiàn)封裝打包自己寫(xiě)的代碼,被python import
- 簡(jiǎn)單了解如何封裝自己的Python包
- python實(shí)現(xiàn)一次性封裝多條sql語(yǔ)句(begin end)
- python將logging模塊封裝成單獨(dú)模塊并實(shí)現(xiàn)動(dòng)態(tài)切換Level方式
- Python發(fā)送郵件封裝實(shí)現(xiàn)過(guò)程詳解
相關(guān)文章
numpy如何按條件給元素賦值np.where、np.clip
這篇文章主要介紹了numpy如何按條件給元素賦值np.where、np.clip問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06Python實(shí)現(xiàn)的幾個(gè)常用排序算法實(shí)例
這篇文章主要介紹了Python實(shí)現(xiàn)的幾個(gè)常用排序算法實(shí)例例如直接插入排序、直接選擇排序、冒泡排序、快速排序等,需要的朋友可以參考下2014-06-06一文講解如何查看python腳本所依賴(lài)三方包及其版本
Python因?yàn)榫哂谐嗟牡谌綆?kù)而被大家喜歡,下面這篇文章主要給大家介紹了關(guān)于如何查看python腳本所依賴(lài)三方包及其版本的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03python使用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關(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