refactor database connection to use SQLAlchemy, update .gitignore for SQLite, and remove MySQL configuration from sample config
This commit is contained in:
parent
fa9f7dfa73
commit
06145634b7
4
.gitignore
vendored
4
.gitignore
vendored
@ -7,7 +7,7 @@ __pycache__/
|
||||
/config.json
|
||||
|
||||
# Ignore the virtual environment directory
|
||||
venv/
|
||||
.venv/
|
||||
|
||||
# Ignore the build directory
|
||||
build/
|
||||
@ -20,3 +20,5 @@ dist/
|
||||
|
||||
# Ignore any .DS_Store files created by macOS
|
||||
.DS_Store
|
||||
|
||||
fastdoi.sqlite3
|
@ -2,9 +2,6 @@
|
||||
"fastgpt_host": "",
|
||||
"fastgpt_api": "",
|
||||
"fastgpt_setId": "",
|
||||
"mysql_host": "",
|
||||
"mysql_user": "",
|
||||
"mysql_password": "",
|
||||
"semAPI": "semanticscholar API",
|
||||
"miniflux_host": "",
|
||||
"miniflux_api": ""
|
||||
|
2
main.py
2
main.py
@ -59,4 +59,4 @@ async def load_rss():
|
||||
if __name__ == '__main__':
|
||||
import uvicorn
|
||||
uvicorn.run(app, host="127.0.0.1", port=8964)
|
||||
mysql_connector.end_mysql()
|
||||
mysql_connector.end_sqlalchemy()
|
@ -2,35 +2,33 @@
|
||||
|
||||
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 = {
|
||||
'user': __CONFIG__['mysql_user'],
|
||||
'password': __CONFIG__['mysql_password'],
|
||||
'host': __CONFIG__['mysql_host'], # Typically 'localhost' if the database is on the same machine
|
||||
'database': 'fastdoi',
|
||||
'raise_on_warnings': True,
|
||||
'auth_plugin':'mysql_native_password'
|
||||
}
|
||||
# SQLite database file path (in the current directory)
|
||||
db_path = os.path.join(os.path.dirname(__file__), 'fastdoi.sqlite3')
|
||||
engine = create_engine(f'sqlite:///{db_path}', echo=False)
|
||||
Session = sessionmaker(bind=engine)
|
||||
session = Session()
|
||||
Base = declarative_base()
|
||||
|
||||
# Establish a connection to the MySQL database
|
||||
cnx = mysql.connector.connect(**mysql_config)
|
||||
cursor = cnx.cursor()
|
||||
class UploadedPaper(Base):
|
||||
__tablename__ = 'uploaded_papers'
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
paperId = Column(String, unique=True, nullable=False)
|
||||
|
||||
def end_mysql():
|
||||
try:
|
||||
# Close the cursor and connection
|
||||
cursor.close()
|
||||
cnx.close()
|
||||
except:
|
||||
print("No Mysql Opened.")
|
||||
# Create the table if it doesn't exist
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
def end_sqlalchemy():
|
||||
session.close()
|
||||
engine.dispose()
|
||||
|
||||
def is_loaded(paperId):
|
||||
query = ("SELECT COUNT(*) FROM uploaded_papers WHERE paperId = %s")
|
||||
cursor.execute(query, (paperId,))
|
||||
return cursor.fetchone()[0] > 0
|
||||
return session.query(UploadedPaper).filter_by(paperId=paperId).count() > 0
|
||||
|
||||
def new_load(paperId):
|
||||
query = ("INSERT INTO uploaded_papers (paperId) VALUES (%s)")
|
||||
cursor.execute(query, (paperId,))
|
||||
cnx.commit()
|
||||
paper = UploadedPaper(paperId=paperId)
|
||||
session.add(paper)
|
||||
session.commit()
|
||||
|
BIN
requirements.txt
Normal file
BIN
requirements.txt
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user