-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathdatabase_controller.py
More file actions
63 lines (50 loc) · 2.44 KB
/
database_controller.py
File metadata and controls
63 lines (50 loc) · 2.44 KB
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
'''
en : All comments were translated using DeepL.
ja : すべてのコメントはDeepLを使用して翻訳されました。
'''
import os, sqlite3, asyncio, sys
db_name = 'database.db' # en:Filename of database ja:データベースのファイル名
table_name = 'translations'
# 実行ファイルのディレクトリを取得(Nuitka/PyInstaller対応)
exe_path = sys.executable
is_frozen = (
getattr(sys, 'frozen', False) or
'__compiled__' in sys.modules or
'/tmp/' in exe_path or '/var/folders/' in exe_path
)
if is_frozen:
# Nuitkaまたはその他のバイナリ実行時
# sys.argv[0]を使用(onefileモードではこちらが正しいパス)
exe_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
else:
# 通常のPythonスクリプト実行時
exe_dir = os.path.dirname(os.path.abspath(__file__))
db_file = os.path.join(exe_dir, db_name) # en:Database File ja:データベース・ファイル
try: # en:Create the database File ja:データベース・ファイルの作成
with open(db_file, "x") as fp: # "x" = en:"Create file" ja:「ファイル作成」
pass
except FileExistsError as e: # en:Continue if file exists ja:ファイルが存在する場合、続行
pass
db = sqlite3.connect(db_file)
cursor = db.cursor()
try: # en:Create translation table ja:翻訳テーブルの作成
sql = "CREATE TABLE translations (ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, MESSAGE TEXT NOT NULL, DLANG TEXT NOT NULL, TRANSLATION TEXT);"
cursor.execute(sql)
except sqlite3.OperationalError as e: # en:Continue if table exists ja:テーブルが存在する場合、続行する
pass
async def save(message,translation,dlang): # en:Save the translations ja:翻訳を保存する
sql = "INSERT INTO translations (MESSAGE,DLANG,TRANSLATION) VALUES (?, ?, ?);"
args = (message, dlang, translation)
cursor.execute(sql, args)
db.commit()
async def get(message,dlang): # en:Get the translations ja:翻訳を入手する
# en:Return translation or None if nothing found ja:翻訳を返すか、何も見つからなければ None を返す
sql = "SELECT TRANSLATION FROM translations WHERE MESSAGE=? AND DLANG=?"
args = (message, dlang)
return cursor.execute(sql, args).fetchone()
def delete(target_size:int = 52428800):
size = os.path.getsize(db_file)
if size >= target_size:
os.remove(db_file)
def close():
db.close()