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

利用Python實(shí)現(xiàn)sqlite3增刪改查的封裝

 更新時(shí)間:2021年12月03日 12:47:28   作者:thinker-master  
在一些小的應(yīng)用中,難免會(huì)用到數(shù)據(jù)庫(kù),Sqlite數(shù)據(jù)庫(kù)以其小巧輕便,無(wú)需安裝,移植性好著稱,下面這篇文章主要給大家介紹了關(guān)于利用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利用函數(shù)式編程實(shí)現(xiàn)優(yōu)化代碼

    Python利用函數(shù)式編程實(shí)現(xiàn)優(yōu)化代碼

    函數(shù)式編程(Functional Programming)是一種編程范式,它將計(jì)算視為函數(shù)的求值,并且避免使用可變狀態(tài)和循環(huán),在Python中還可以利用它的簡(jiǎn)潔和高效來(lái)解決實(shí)際問(wèn)題,下面我們就來(lái)學(xué)習(xí)一下它的具體用法吧
    2023-11-11
  • Python讀取YAML文件過(guò)程詳解

    Python讀取YAML文件過(guò)程詳解

    這篇文章主要介紹了Python讀取YAML文件過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • 基于Python實(shí)現(xiàn)文件大小輸出

    基于Python實(shí)現(xiàn)文件大小輸出

    在數(shù)據(jù)庫(kù)中存儲(chǔ)時(shí),使用 Bytes 更精確,可擴(kuò)展性和靈活性都很高。下面通過(guò)本文給大家介紹基于Python實(shí)現(xiàn)文件大小輸出,對(duì)python文件輸出相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧
    2016-01-01
  • 使用python實(shí)現(xiàn)回文數(shù)的四種方法小結(jié)

    使用python實(shí)現(xiàn)回文數(shù)的四種方法小結(jié)

    今天小編就為大家分享一篇使用python實(shí)現(xiàn)回文數(shù)的四種方法小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-11-11
  • 詳解Python如何獲取列表(List)的中位數(shù)

    詳解Python如何獲取列表(List)的中位數(shù)

    本文通過(guò)圖文及實(shí)例代碼介紹了怎樣利用python獲取列表的中位數(shù),文章介紹的很詳細(xì),有需要的小伙伴們可以參考學(xué)習(xí)。
    2016-08-08
  • Python使用pdb調(diào)試代碼的技巧

    Python使用pdb調(diào)試代碼的技巧

    Pdb就是Python debugger,是python自帶的調(diào)試器。這篇文章主要介紹了Python使用pdb調(diào)試代碼的技巧,需要的朋友可以參考下
    2020-05-05
  • python入門(mén)之算法學(xué)習(xí)

    python入門(mén)之算法學(xué)習(xí)

    這篇文章主要介紹了python入門(mén)之算法學(xué)習(xí),文中介紹的非常詳細(xì),對(duì)想要入門(mén)python的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-04-04
  • python如何代碼集體右移

    python如何代碼集體右移

    在本篇文章里小編給各位分享的是一篇關(guān)于python如何代碼集體右移的相關(guān)知識(shí)點(diǎn)文章,需要的朋友們可以學(xué)習(xí)下。
    2020-07-07
  • python模擬嗶哩嗶哩滑塊登入驗(yàn)證的實(shí)現(xiàn)

    python模擬嗶哩嗶哩滑塊登入驗(yàn)證的實(shí)現(xiàn)

    這篇文章主要介紹了python模擬嗶哩嗶哩滑塊登入驗(yàn)證的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Linux中Python 環(huán)境軟件包安裝步驟

    Linux中Python 環(huán)境軟件包安裝步驟

    本文給大家分享的是在Linux系統(tǒng)中Python環(huán)境的安裝步驟,以及常用的軟件的安裝升級(jí),非常的實(shí)用,有需要的小伙伴可以參考下
    2016-03-03

最新評(píng)論