EnglishPal/app/MyPony.py

56 lines
1.2 KiB
Python

from pony.orm import *
db = Database()
# 数据库实体
class Article(db.Entity):
_table_ = "article" # table name
article_id = PrimaryKey(int, auto=True)
text = Optional(str)
source = Optional(str)
date = Optional(str)
level = Optional(str)
question = Optional(str)
db.bind(provider='sqlite', filename='./static/wordfreqapp.db', create_db=True)
db.generate_mapping(create_tables=True)
# 打印SQL错误
set_sql_debug(True)
@db_session
def selectAllArticles():
"""
获取全部文章
"""
articles = select(a for a in Article)
articles_list = []
for i in articles:
d = {"article_id": i.article_id,
"text": i.text,
"source": i.source,
"date": i.date,
"level": i.level,
"question": i.question}
articles_list.append(d)
return articles_list
@db_session
def selectArticlesById(id):
"""
根据文章id搜索
"""
article = Article.get(article_id=id)
li = []
d = {"article_id": article.article_id,
"text": article.text,
"source": article.source,
"date": article.date,
"level": article.level,
"question": article.question}
li.append(d)
return li