import os import sys import sqlite3 import UtilPack as util class MgrCalibreDB: def __init__(self, path): self.path = path def __enter__(self): pass def __exit__(self, ex_type, ex_value, traceback): self.conn.close() def init(self): # 데이터베이스 연결 (파일이 없으면 새로 생성됨) self.conn = sqlite3.connect(self.path) self.conn.create_function("title_sort", 1, self._title_sort ) self.cursor = self.conn.cursor() def _title_sort(self, value): return 0 def GetTableAll(self, table): if None == self.cursor: return None if None == table or True == util.IsEmptyStr(table): return None strSQL = f"SELECT * FROM \"{table}\";" self.cursor.execute(strSQL) listRet = self.cursor.fetchall() return listRet def GetTableSchm(self, table): if None == self.cursor: return None if None == table or True == util.IsEmptyStr(table): return None strSQL = f"PRAGMA table_info (\"{table}\");" self.cursor.execute(strSQL) listRet = self.cursor.fetchall() return listRet def GetBookListforUI_ArcList(self): if None == self.cursor: return None try: strSQL = f"Select title, path from books;" self.cursor.execute(strSQL) listRet = self.cursor.fetchall() except sqlite3.Error as e: listRet = [] print(f"SQLite Erro occurred: {e}") return listRet def UpdateTableValue(self,Table, nID, ColName, Value): if None == self.cursor: return if 0 >= nID or True == util.IsEmptyStr(Table) or True == util.IsEmptyStr(ColName): return strSQL = f"UPDATE {Table} SET \'{ColName}\' = \'{Value}\' WHERE id = {nID};" self.cursor.execute(strSQL) self.conn.commit() def InsertAuthor(self, strAuthor): if None == self.cursor: return if True == util.IsEmptyStr(strAuthor): return strTableName = "authors" strSQL = f"SELECT MAX(id) FROM \'{strTableName}\';" self.cursor.execute(strSQL) nMaxID = self.cursor.fetchone()[0] nMaxID += 1 strAuthorSort = strAuthor.replace(" ", ",") strSQL = f"INSERT OR IGNORE INTO \'{strTableName}\' VALUES ({nMaxID}, \'{strAuthor}\',\'{strAuthorSort}\', \'\');" self.cursor.execute(strSQL) self.conn.commit() def InsertTag(self, tag): if None == self.cursor: return if True == util.IsEmptyStr(tag): return strTableName = "tags" strSQL = f"SELECT MAX(id) FROM \'{strTableName}\';" self.cursor.execute(strSQL) nMaxID = self.cursor.fetchone()[0] nMaxID += 1. strSQL = f"INSERT OR IGNORE INTO \'{strTableName}\' VALUES ({nMaxID}, \'{strAuthor}\', \'\');" self.cursor.execute(strSQL) self.conn.commit() """ def main(): db = MgrCalibreDB("/Users/minarinari/캘리버 서재/metadata.db") db.init() #db.UpdateTableValue("books", 3, "title", "삭막") db.InsertAuthor("Oyster") db.InsertAuthor("Rustle") db.InsertAuthor("ShindoL") listValues = db.GetTableAll("authors") print(listValues) # For Main Loop if __name__ == '__main__': main() """ # 테이블 생성 #cursor.execute('''CREATE TABLE IF NOT EXISTS users # (id INTEGER PRIMARY KEY AUTOINCREMENT, # name TEXT NOT NULL, # age INTEGER, # email TEXT)''') # 데이터 삽입 #cursor.execute("INSERT INTO users (name, age, email) VALUES (?, ?, ?)", # ("Alice", 30, "alice@example.com")) #conn.commit() # 데이터 조회 # cursor.execute("SELECT * FROM books") # rows = cursor.fetchall() # for nRow in range(0, len(rows)): # row = rows[nRow] # for nCol in range(0,len(row)): # item = row[nCol] # print(f"{nRow}:{nCol} -> {item}") # print("\r\n") # 데이터 업데이트 #cursor.execute("UPDATE users SET age = ? WHERE name = ?", (31, "Alice")) #conn.commit() # 데이터 삭제 #cursor.execute("DELETE FROM users WHERE name = ?", ("Alice",)) #conn.commit() # 연결 닫기 #conn.close()