利用Python實(shí)現(xiàn)sqlite3增刪改查的封裝
開(kāi)發(fā)背景:
每次項(xiàng)目都要寫(xiě)數(shù)據(jù)庫(kù)、煩死了。。然后就每次數(shù)據(jù)庫(kù)都要花很多時(shí)間。煩死了!不如寫(xiě)個(gè)通用的增刪查改,以不變應(yīng)萬(wàn)變!
特性:
- 搭建通用增刪查改模塊,減少代碼量。
- 讓代碼更加清晰、可讀
- 即便進(jìn)行了封裝,也絲毫不影響其靈活性
sqlite3我也就不多介紹了,直接上代碼。附上相關(guān)使用方法和測(cè)試用例!
使用方法
import sqlite3
'''寫(xiě)一個(gè)類打包成庫(kù),通用于儲(chǔ)存信息的sqlite'''
'''函數(shù)返回值可優(yōu)化'''
'''使用:使用'''
'''說(shuō)明:1、單例模式連接數(shù)據(jù)庫(kù):避免數(shù)據(jù)庫(kù)connect過(guò)多導(dǎo)致數(shù)據(jù)庫(kù)down
2、根據(jù)數(shù)據(jù)庫(kù)增刪查改性能對(duì)比,統(tǒng)一使用execute進(jìn)行常規(guī)數(shù)據(jù)庫(kù)操作
3、且不做try操作:1、影響性能 2、若報(bào)錯(cuò),外部調(diào)用無(wú)法確定問(wèn)題所在,'''
class LiteDb(object):
_instance = None
def __new__(cls, *args, **kw):
if cls._instance is None:
cls._instance = object.__new__(cls)
return cls._instance
def openDb(self, dbname):
self.dbname = dbname
self.conn = sqlite3.connect(self.dbname)
self.cursor = self.conn.cursor()
def closeDb(self):
'''
關(guān)閉數(shù)據(jù)庫(kù)
:return:
'''
self.cursor.close()
self.conn.close()
def createTables(self, sql):
'''
example:'create table userinfo(name text, email text)'
:return: result=[1,None]
'''
self.cursor.execute(sql)
self.conn.commit()
result = [1, None]
return result
def dropTables(self, sql):
'''
example:'drop table userinfo'
:param sql:
:return:result=[1,None]
'''
self.cursor.execute(sql)
self.conn.commit()
result = [1, None]
return result
def executeSql(self, sql, value=None):
'''
執(zhí)行單個(gè)sql語(yǔ)句,只需要傳入sql語(yǔ)句和值便可
:param sql:'insert into user(name,password,number,status) values(?,?,?,?)'
'delete from user where name=?'
'updata user set status=? where name=?'
'select * from user where id=%s'
:param value:[(123456,123456,123456,123456),(123,123,123,123)]
value:'123456'
value:(123,123)
:return:result=[1,None]
'''
'''增、刪、查、改'''
if isinstance(value,list) and isinstance(value[0],(list,tuple)):
for valu in value:
self.cursor.execute(sql, valu)
else:
self.conn.commit()
result = [1, self.cursor.fetchall()]
else:
'''執(zhí)行單條語(yǔ)句:字符串、整型、數(shù)組'''
if value:
self.cursor.execute(sql, value)
else:
self.cursor.execute(sql)
self.conn.commit()
result = [1, self.cursor.fetchall()]
return result
測(cè)試用例
from dbUse import LiteDb
'''對(duì)于二次封裝的數(shù)據(jù)庫(kù)進(jìn)行測(cè)試'''
'''增刪查改'''
#用例
'''
select name from sqlite_master where type='table
select * from user
'select * from user where id = %s'%7
select * from user where id = ? , 7
select * from user where id=? or id=?, ['7','8']
insert into user(id) values(7)
'insert into user(id) values(%s)'%7
'insert into user(id) values(?)',[('10',),('11',)]
delete from user where id=7
'delete from user where id=%s'%7
'delete from user where id=?',[('10',),('11',)]
update user set id=7 where id=11
'update user set id=%s where id=%s'%(10,20)
'update user set id=? where id=?',[('21','11'),('20','10')]'''
db=LiteDb()
db.openDb('user.db')
def close():
db.closeDb()
def createTables():
result=db.createTables('create table if not exists user(id varchar(128))')
def executeSQL():
'''增刪查改'''
result = db.executeSql('insert into user(id) values(?)',('99',))
result = db.executeSql('select * from user ')
executeSQL()
close()
Python參數(shù)傳遞方式
Python的參數(shù)傳遞一共有以下五種(位置參數(shù)、默認(rèn)參數(shù)、變長(zhǎng)參數(shù)、關(guān)鍵字參數(shù)、命名關(guān)鍵字參數(shù))
位置傳遞,即參數(shù)按照定義的位置及順序進(jìn)行傳遞,如下所示:
# 位置傳遞實(shí)例:
def fun1(a, b, c):
return a + b + c
print(fun1(1, 2, 3))
關(guān)鍵字傳遞,即通過(guò)傳遞的參數(shù)的名稱進(jìn)行識(shí)別。
# 關(guān)鍵字傳遞 def fun2(a, b, c): return a + b + c print(fun2(1, c=3, b=2))
默認(rèn)值參數(shù)傳遞,即給某些參數(shù)設(shè)置一個(gè)默認(rèn)值,如果不傳則讀取默認(rèn)值。
# 默認(rèn)值傳遞 def fun3(a, b=2, c=3): return a + b + c print(fun3(a=1))
元組傳遞,在定義函數(shù)時(shí),我們有時(shí)候并不知道調(diào)用的時(shí)候會(huì)傳遞多少個(gè)參數(shù)。元組參數(shù)來(lái)進(jìn)行參數(shù)傳遞會(huì)非常有用。如下所示:
def fun4(*name):
print(type(name))
print(name)
fun4((1, 2, 3))
字典傳遞,雖然通過(guò)元組可以傳遞多個(gè)參數(shù),但如果需要通過(guò)鍵值來(lái)獲取參數(shù)內(nèi)容時(shí),字典則更加方便,如下所示:
def fun5(a, b, **kwargs): print(type(kwargs)) # <class 'dict'> print(a, b, kwargs) fun5(2, 3, name='Alan.hsiang', age=23)
總結(jié)
到此這篇關(guān)于利用Python實(shí)現(xiàn)sqlite3增刪改查封裝的文章就介紹到這了,更多相關(guān)python?sqlite3增刪改查內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python之tensorflow手把手實(shí)例講解貓狗識(shí)別實(shí)現(xiàn)
要說(shuō)到深度學(xué)習(xí)圖像分類的經(jīng)典案例之一,那就是貓狗大戰(zhàn)了。貓和狗在外觀上的差別還是挺明顯的,無(wú)論是體型、四肢、臉龐和毛發(fā)等等, 都是能通過(guò)肉眼很容易區(qū)分的。那么如何讓機(jī)器來(lái)識(shí)別貓和狗呢?網(wǎng)上已經(jīng)有不少人寫(xiě)過(guò)這案例了,我也來(lái)嘗試下練練手。2021-09-09
python實(shí)現(xiàn)圖像增強(qiáng)算法
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)圖像增強(qiáng)算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
更改Python的pip install 默認(rèn)安裝依賴路徑方法詳解
今天小編就為大家分享一篇更改Python的pip install 默認(rèn)安裝依賴路徑方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
Django框架中間件(Middleware)用法實(shí)例分析
這篇文章主要介紹了Django框架中間件(Middleware)用法,結(jié)合實(shí)例形式分析了Django框架中間件(Middleware)的功能、用法及相關(guān)操作技巧,需要的朋友可以參考下2019-05-05
Python用內(nèi)置模塊來(lái)構(gòu)建REST服務(wù)與RPC服務(wù)實(shí)戰(zhàn)
這篇文章主要介紹了Python用內(nèi)置模塊來(lái)構(gòu)建REST服務(wù)與RPC服務(wù)實(shí)戰(zhàn),python在網(wǎng)絡(luò)方面封裝一些內(nèi)置模塊,可以用很簡(jiǎn)潔的代碼實(shí)現(xiàn)端到端的通信,比如HTTP、RPC服務(wù),下文實(shí)戰(zhàn)詳情,需要的朋友可以參考一下2022-09-09

