qnloft-stock/DB/sqlite_db_main.py

51 lines
1.7 KiB
Python
Raw Normal View History

2023-11-24 02:49:22 +00:00
from pathlib import Path
from DB.db_main import *
import platform
import logging
class SqliteDbMain(DbMain):
def __init__(self, database_name):
# logging.basicConfig()
# logging.getLogger('sqlalchemy.engine').setLevel(logging.ERROR)
# logging.getLogger('sqlalchemy.pool').setLevel(logging.ERROR)
# logging.getLogger('sqlalchemy.dialects').setLevel(logging.ERROR)
# logging.getLogger('sqlalchemy.orm').setLevel(logging.ERROR)
self.database_name = database_name
super().__init__()
self.engine = self.__create_sqlite_engine()
self.engine_path = self.__get_path()
self.session = self.get_session()
self.inspector = inspect(self.engine)
def __get_path(self):
sys_platform = platform.platform().lower()
print(f'当前操作系统:{platform.platform()}')
__engine = ''
if 'windows' in sys_platform.lower():
__engine = f"E:\\sqlite_db\\stock_db\\{self.database_name}"
elif 'macos' in sys_platform.lower():
__engine = f"/Users/renmeng/Documents/sqlite_db/{self.database_name}"
else:
__engine = f"{self.database_name}"
return __engine
def __create_sqlite_engine(self):
sys_platform = platform.platform().lower()
__engine = ''
if 'windows' in sys_platform.lower():
__engine = f"sqlite:///E:\\sqlite_db\\stock_db\\{self.database_name}"
elif 'macos' in sys_platform.lower():
__engine = f"sqlite:////Users/renmeng/Documents/sqlite_db/{self.database_name}"
else:
__engine = f"sqlite:///{self.database_name}"
print(f"当前__engine是{__engine}")
return create_engine(__engine, pool_size=10, pool_timeout=10, echo=True)
def get_db_size(self):
file_size = Path(self.engine_path).stat().st_size
total = f"{file_size / (1024 * 1024):.2f} MB"
print(f"文件大小: {file_size / (1024 * 1024):.2f} MB")
return total