Python 操作sqllite

发布于:2024-03-21 ⋅ 阅读:(57) ⋅ 点赞:(0)

共有5个字段,实现增、查、改功能

import sqlite3
import threading
import functools

PATH_DATABASE = 'threelang.db'

def synchronized(func):
  @functools.wraps(func)
  def wrapper(self, *args, **kwargs):
    with self.lock:
      return func(self, *args, **kwargs)
  return wrapper
class Authorize_Tb:

    def __init__(self) -> None:
        self.lock = threading.Lock()
           
    #初始化
    def init_tb(self):
        conn = sqlite3.connect(PATH_DATABASE)
        c = conn.cursor()
        try:
            c.execute('SELECT * FROM T_ThreeLang')
        except sqlite3.OperationalError:
            c.execute('''
                CREATE TABLE T_ThreeLang
                (id INTEGER PRIMARY KEY     autoincrement,
                chinese        TEXT,
                thai           TEXT,
                english        TEXT,
                act             TEXT,
                times         Int);
            ''')
            conn.commit()
        finally:
            conn.close()

    #添加
    @synchronized
    def add(self,chinese,thai,english,act,times=0):
        conn = sqlite3.connect(PATH_DATABASE)
        cur = conn.cursor()
        cur.execute("insert into T_ThreeLang (chinese,thai,english,act,times) values (?,?,?,?,?)",(chinese,thai,english,act,times))
        conn.commit()
        conn.close()
        return cur.lastrowid   

    #查询 ('你好', 'สวัสดี', 'Hello', 'act',0) None
    @synchronized
    def find_by_chinese(self,chinese):
        conn = sqlite3.connect(PATH_DATABASE)
        cur = conn.cursor()
        cur.execute("select id,chinese,thai,english,act,times from T_ThreeLang where chinese = ? order by id desc limit 1",(chinese,))
        info = cur.fetchone()
        conn.close()
        return info
    
    #查询
    @synchronized
    def find_by_id(self,id):
        conn = sqlite3.connect(PATH_DATABASE)
        cur = conn.cursor()
        cur.execute("select id,chinese,thai,english,act,times from T_ThreeLang where id = ? order by id desc limit 1",(id,))
        info = cur.fetchone()
        conn.close()
        return info

    # 更新
    @synchronized
    def update_by_id(self, id,thai,english,act,times):
        conn = sqlite3.connect(PATH_DATABASE)
        cur = conn.cursor()
        cur.execute("UPDATE T_ThreeLang SET thai = ?, english = ?, act = ?, times = ? WHERE id = ?", 
                    (thai, english, act, times, id))
        conn.commit()
        conn.close()

    # 更新
    @synchronized
    def update_by_chinese(self, chinese,thai,english,act,times):
        conn = sqlite3.connect(PATH_DATABASE)
        cur = conn.cursor()
        cur.execute("UPDATE T_ThreeLang SET thai = ?, english = ?, act = ?, times = ? WHERE chinese = ?", 
                    (thai, english, act, times, chinese))
        conn.commit()
        conn.close()

    # Count
    @synchronized
    def count(self):
        conn = sqlite3.connect(PATH_DATABASE)
        cur = conn.cursor()
        cur.execute("select count(*) as number from T_ThreeLang")
        info = cur.fetchone()
        conn.close()
        return info

if __name__ == '__main__':
    db = Authorize_Tb()
    #db.init_tb()
    info = db.find_by_chinese('你好')
    if not info:
        id = db.add('你好','สวัสดี','Hello', 'act',0)
        print(id)

网站公告

今日签到

点亮在社区的每一天
去签到