diff --git a/app.py b/app.py index ff2a6c774..3090402ed 100644 --- a/app.py +++ b/app.py @@ -8,6 +8,7 @@ from channel import channel_factory from common import const from config import load_config +from lib.db.frame import DB from plugins import * import threading @@ -43,12 +44,13 @@ def start_channel(channel_name: str): def run(): try: # load config - load_config() + config = load_config() # ctrl + c sigterm_handler_wrap(signal.SIGINT) # kill signal sigterm_handler_wrap(signal.SIGTERM) - + + DB.db_connect(config) # create channel channel_name = conf().get("channel_type", "wx") diff --git a/config.py b/config.py index 50d2fbd63..609501066 100644 --- a/config.py +++ b/config.py @@ -175,6 +175,7 @@ "Minimax_api_key": "", "Minimax_group_id": "", "Minimax_base_url": "", + "SQLALCHEMY_DATABASE_URI": "", } @@ -295,6 +296,7 @@ def load_config(): logger.info("[INIT] load config: {}".format(drag_sensitive(config))) config.load_user_datas() + return config def get_root(): diff --git a/lib/db/frame.py b/lib/db/frame.py new file mode 100644 index 000000000..a7bad622f --- /dev/null +++ b/lib/db/frame.py @@ -0,0 +1,53 @@ +import os +from sqlalchemy import create_engine, Column, Integer, String, Sequence +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import sessionmaker + +class DB: + session = None + """ + A method to establish a connection to the database, create necessary tables, and initialize a session. + """ + @classmethod + def db_connect(cls, config): + # Create an engine + db_config = config.get("SQLALCHEMY_DATABASE_URI", "sqlite:///wechat_bot_dev.db") + engine = create_engine(db_config) + + # Define the base + Base = declarative_base() + + # Create the tables + Base.metadata.create_all(engine) + + # Create a session + Session = sessionmaker(bind=engine) + cls.session = Session() + + @classmethod + def get_session(cls): + return cls.session + + +# Create a new user +# new_user = User(name='John', fullname='John Doe', nickname='johnny') +# session.add(new_user) +# session.commit() + +# # Query all users +# users = session.query(User).all() +# for user in users: +# print(user.name, user.fullname, user.nickname) + +# # Update a user +# user = session.query(User).filter_by(name='John').first() +# user.nickname = 'johnny_d' +# session.commit() + +# # Delete a user +# user_to_delete = session.query(User).filter_by(name='John').first() +# session.delete(user_to_delete) +# session.commit() + +# # Close the session +# session.close()