forked from SunoApi/SunoApi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite.py
169 lines (167 loc) · 5.65 KB
/
sqlite.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# -*- coding:utf-8 -*-
import sys
import os
import sqlite3
import threading
class SqliteTool():
"""
简单sqlite数据库工具类
编写这个类主要是为了封装sqlite,继承此类复用方法
"""
def __init__(self, dbName="sunoapi.db"):
"""
初始化连接——使用完需关闭连接
:param dbName: 连接库的名字,注意,以'.db'结尾
"""
# # 连接数据库
# conn = sqlite3.connect(dbName, check_same_thread = False)
# # 创建游标
# cur = conn.cursor()
def create_conn(self):
return sqlite3.connect('sunoapi.db', isolation_level=None)
# # 创建数据表
# def create_tabel(self, sql: str):
# """
# 创建表
# :param sql: create sql语句
# :return: True表示创建表成功
# """
# try:
# conn = self.create_conn()
# with conn:
# cur = conn.cursor()
# cur.execute(sql)
# conn.commit()
# print("[create table success]")
# return True
# except Exception as e:
# print("[create table error]", e)
# # 删除数据表
# def drop_table(self, sql: str):
# """
# 删除表
# :param sql: drop sql语句
# :return: True表示删除成功
# """
# try:
# conn = self.create_conn()
# with conn:
# cur = conn.cursor()
# cur.execute(sql)
# conn.commit()
# return True
# except Exception as e:
# print("[drop table error]", e)
# return False
# 插入或更新表数据,一次插入或更新一条数据
def operate_one(self, sql: str, value: tuple):
"""
插入或更新单条表记录
:param sql: insert语句或update语句
:param value: 插入或更新的值,形如()
:return: True表示插入或更新成功
"""
try:
conn = self.create_conn()
with conn:
cur = conn.cursor()
cur.execute(sql, value)
conn.commit()
if 'INSERT' in sql.upper():
pass
# print(f"[insert one record success]:{sql}")
if 'UPDATE' in sql.upper():
pass
# print(f"[update one record success]:{sql}")
return True
except Exception as e:
print(f"[insert/update one record error]:{sql}", e)
return False
# 插入或更新表数据,一次插入或更新多条数据
def operate_many(self, sql: str, value: list):
"""
插入或更新多条表记录
:param sql: insert语句或update语句
:param value: 插入或更新的字段的具体值,列表形式为list:[(),()]
:return: True表示插入或更新成功
"""
try:
conn = self.create_conn()
with conn:
cur = conn.cursor()
cur.executemany(sql, value)
conn.commit()
if 'INSERT' in sql.upper():
print(f"[insert many records success]:{sql}")
if 'UPDATE' in sql.upper():
print(f"[update many records success]:{sql}")
return True
except Exception as e:
print(f"[insert/update many records error]:{sql}", e)
return False
# 删除表数据
def delete_record(self, sql: str):
"""
删除表记录
:param sql: 删除记录SQL语句
:return: True表示删除成功
"""
try:
conn = self.create_conn()
with conn:
cur = conn.cursor()
if 'DELETE' in sql.upper():
cur.execute(sql)
conn.commit()
print(f"[detele record success]:{sql}")
return True
else:
print(f"[sql is not delete]:{sql}")
return False
except Exception as e:
print(f"[detele record error]:{sql}", e)
return False
# 查询一条数据
def query_one(self, sql: str, params=None):
"""
查询单条数据
:param sql: select语句
:param params: 查询参数,形如()
:return: 语句查询单条结果
"""
try:
conn = self.create_conn()
with conn:
cur = conn.cursor()
if params:
cur.execute(sql, params)
else:
cur.execute(sql)
# 调用fetchone()方法
r = cur.fetchone()
# print(f"[select one record success]:{sql}")
return r
except Exception as e:
print(f"[select one record error]:{sql}", e)
# 查询多条数据
def query_many(self, sql: str, params=None):
"""
查询多条数据
:param sql: select语句
:param params: 查询参数,形如()
:return: 语句查询多条结果
"""
try:
conn = self.create_conn()
with conn:
cur = conn.cursor()
if params:
cur.execute(sql, params)
else:
cur.execute(sql)
# 调用fetchall()方法
r = cur.fetchall()
print(f"[select many records success]:{sql}")
return r
except Exception as e:
print(f"[select many records error]:{sql}", e)