refactor database connection to use SQLAlchemy, update .gitignore for SQLite, and remove MySQL configuration from sample config

This commit is contained in:
Miu Li 2025-06-19 05:22:24 +08:00
parent fa9f7dfa73
commit 06145634b7
5 changed files with 27 additions and 30 deletions

4
.gitignore vendored
View File

@ -7,7 +7,7 @@ __pycache__/
/config.json /config.json
# Ignore the virtual environment directory # Ignore the virtual environment directory
venv/ .venv/
# Ignore the build directory # Ignore the build directory
build/ build/
@ -20,3 +20,5 @@ dist/
# Ignore any .DS_Store files created by macOS # Ignore any .DS_Store files created by macOS
.DS_Store .DS_Store
fastdoi.sqlite3

View File

@ -2,9 +2,6 @@
"fastgpt_host": "", "fastgpt_host": "",
"fastgpt_api": "", "fastgpt_api": "",
"fastgpt_setId": "", "fastgpt_setId": "",
"mysql_host": "",
"mysql_user": "",
"mysql_password": "",
"semAPI": "semanticscholar API", "semAPI": "semanticscholar API",
"miniflux_host": "", "miniflux_host": "",
"miniflux_api": "" "miniflux_api": ""

View File

@ -59,4 +59,4 @@ async def load_rss():
if __name__ == '__main__': if __name__ == '__main__':
import uvicorn import uvicorn
uvicorn.run(app, host="127.0.0.1", port=8964) uvicorn.run(app, host="127.0.0.1", port=8964)
mysql_connector.end_mysql() mysql_connector.end_sqlalchemy()

View File

@ -2,35 +2,33 @@
from config import __CONFIG__ from config import __CONFIG__
import mysql.connector from sqlalchemy import create_engine, Column, String, Integer
from sqlalchemy.orm import sessionmaker, declarative_base
import os
mysql_config = { # SQLite database file path (in the current directory)
'user': __CONFIG__['mysql_user'], db_path = os.path.join(os.path.dirname(__file__), 'fastdoi.sqlite3')
'password': __CONFIG__['mysql_password'], engine = create_engine(f'sqlite:///{db_path}', echo=False)
'host': __CONFIG__['mysql_host'], # Typically 'localhost' if the database is on the same machine Session = sessionmaker(bind=engine)
'database': 'fastdoi', session = Session()
'raise_on_warnings': True, Base = declarative_base()
'auth_plugin':'mysql_native_password'
}
# Establish a connection to the MySQL database class UploadedPaper(Base):
cnx = mysql.connector.connect(**mysql_config) __tablename__ = 'uploaded_papers'
cursor = cnx.cursor() id = Column(Integer, primary_key=True, autoincrement=True)
paperId = Column(String, unique=True, nullable=False)
def end_mysql(): # Create the table if it doesn't exist
try: Base.metadata.create_all(engine)
# Close the cursor and connection
cursor.close() def end_sqlalchemy():
cnx.close() session.close()
except: engine.dispose()
print("No Mysql Opened.")
def is_loaded(paperId): def is_loaded(paperId):
query = ("SELECT COUNT(*) FROM uploaded_papers WHERE paperId = %s") return session.query(UploadedPaper).filter_by(paperId=paperId).count() > 0
cursor.execute(query, (paperId,))
return cursor.fetchone()[0] > 0
def new_load(paperId): def new_load(paperId):
query = ("INSERT INTO uploaded_papers (paperId) VALUES (%s)") paper = UploadedPaper(paperId=paperId)
cursor.execute(query, (paperId,)) session.add(paper)
cnx.commit() session.commit()

BIN
requirements.txt Normal file

Binary file not shown.