Python-sqlite3 常用總結(jié)

Python 關(guān)于sqlite3的簡單學(xué)習(xí)和總結(jié)

  • SQLite 是一個(gè)小型嵌入式 SQL 數(shù)據(jù)庫引擎。與大多數(shù)其他 SQL 數(shù)據(jù)庫不同,SQLite 沒有單獨(dú)的服務(wù)器進(jìn)程。SQLite 直接讀寫普通磁盤文件。具有多個(gè)表、索引、觸發(fā)器和視圖,完整 SQL 數(shù)據(jù)庫包含在單個(gè)磁盤文件中。在小型應(yīng)用軟件開發(fā)中經(jīng)常使用該數(shù)據(jù)庫便于快速開發(fā)項(xiàng)目,由于其輕量級特性,操作簡單因此應(yīng)用廣泛。
  • 由于項(xiàng)目中需要使用持久化存儲(chǔ)數(shù)據(jù)應(yīng)用到sqlite3,本人在學(xué)習(xí)之余,總結(jié)記錄了一些學(xué)習(xí)筆記,便于日后歸納總結(jié)建立完整的知識(shí)體系。
  • demo中將sqlite 的簡單操作封裝到類里,重點(diǎn)在于學(xué)習(xí)SQL的語句。
#-*- coding=utf-8 -*-
import sqlite3

class sqltest:
    def __init__(self,path):
        self.dbpath = path
        self.cnn = sqlite3.connect(path)
        self.cursor = self.cnn.cursor()

    def sqlcmd(self,cmdline):
        return self.cursor.execute(cmdline)

    def commit(self):
        self.cnn.commit()
    def rollback(self):
        self.cnn.rollback()

    def close(self):
        self.cursor.close()
        self.cnn.close()

    def connect_test(path):
        return sqlite3.connect(path)

    def fetch(self):
        return self.cursor.fetchone()
    
    def fetchAll(self):
        return self.cursor.fetchall()

if __name__ == '__main__':


    print("---------開始----------")
    #數(shù)據(jù)庫文件地址
    DBPATH = r'D:\demo_test\sql_test\sqldb.db'
    #當(dāng)數(shù)據(jù)庫不存在時(shí),會(huì)新建數(shù)據(jù)庫
    cnn = sqltest(DBPATH)
    #sqlline = 'create table if not exists table1(name string primary key,age int,sex string)'
    sqlline = 'create table if not exists %s'%('table1')+'(name string primary key,age int,sex string)'
    
    
    #創(chuàng)建表
    cnn.sqlcmd(sqlline)
    #插入3個(gè)數(shù)據(jù)
    cnn.sqlcmd("insert into table1 values('kangkang',20,'man')")
    cnn.sqlcmd("insert into table1 values('mike',22,'man')")
    cnn.sqlcmd("insert into table1 values('jane',18,'woman')")
    #提交數(shù)據(jù)到數(shù)據(jù)庫
    cnn.commit()
    #查詢mike
    cnn.sqlcmd("select * from table1 where name == 'mike'")
    tb = cnn.fetchAll()
    print("查詢:",tb)
    
    #修改mike的性別
    cnn.sqlcmd("update table1 set age = 20 where name == 'mike'")
    cnn.commit()
    cnn.sqlcmd("select * from table1 where name == 'mike'")
    tb = cnn.fetchAll()
    print("修改:",tb)

    #回滾
    cnn.rollback()
    cnn.sqlcmd("select * from table1 where name == 'mike'")
    tb = cnn.fetchAll()
    print("回滾:",tb)

    #刪除掉mike
    cnn.sqlcmd("delete from table1 where name == 'mike'")
    cnn.commit()
    cnn.sqlcmd("select * from table1 where name == 'mike'")
    tb = cnn.fetchAll()
    print("刪除:",tb)

    #刪除表
    #cnn.sqlcmd("drop table table1")
    cnn.close()

  • 打印輸出結(jié)果如下
---------開始----------
查詢: [('kangkang', 20, 'man'), ('mike', 22, 'man'), ('jane', 18, 'woman')]
修改: [('mike', 20, 'man')]
回滾: [('mike', 20, 'man')]
刪除: [('kangkang', 20, 'man'), ('jane', 18, 'woman')]
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容