2024-01-16 15:33:13 +00:00
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
from qnloft_db.db_main import *
|
|
|
|
|
import platform
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SqliteDbMain(DbMain):
|
|
|
|
|
def __init__(self, database_name):
|
|
|
|
|
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:
|
2024-01-17 10:22:04 +00:00
|
|
|
|
__engine = f"../sqlite_db/{self.database_name}"
|
2024-01-16 15:33:13 +00:00
|
|
|
|
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
|