Browse Source

Add utils/db_utils

StrayWarrior 3 weeks ago
parent
commit
767dcb5521
1 changed files with 12 additions and 0 deletions
  1. 12 0
      pqai_agent/utils/db_utils.py

+ 12 - 0
pqai_agent/utils/db_utils.py

@@ -0,0 +1,12 @@
+from urllib.parse import quote_plus
+
+from sqlalchemy import create_engine
+
+def create_sql_engine(config):
+    user = config['user']
+    passwd = quote_plus(config['password'])
+    host = config['host']
+    db_name = config['database']
+    charset = config.get('charset', 'utf8mb4')
+    engine = create_engine(f'mysql+pymysql://{user}:{passwd}@{host}/{db_name}?charset={charset}')
+    return engine