Python 操作sqllite

共有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)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值