-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_connect.py
46 lines (38 loc) · 1.17 KB
/
db_connect.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
import sqlite3
# DB接続
class ObjectRelationalMapping:
def __init__(self, db_path: str) -> None:
self.db_path = db_path
self.connect = None
self.cursor = None
self.db_cursor()
# DB接続
def db_connect(self):
if self.connect is not None:
self.db_close()
self.connect = sqlite3.connect(self.db_path)
# カーソルを開く
def db_cursor(self):
if self.connect is None:
self.db_connect()
if self.cursor is None:
self.cursor = self.connect.cursor()
# DBコミット(確定)
def db_commit(self):
self.connect.commit()
# SQL実行:パラメータ無し
def db_execute(self, sql):
if self.cursor is None:
self.db_cursor()
return self.cursor.execute(sql)
# SQL実行:パラメータ付き
def db_execute_many(self, sql, data):
if self.cursor is None:
self.db_cursor()
return self.connect.executemany(sql, data)
# DB接続切断
def db_close(self):
if self.connect is not None:
self.connect.close()
self.cursor = None
self.connect = None