news_server/mod/news.py
2019-12-29 21:33:37 +08:00

37 lines
1.0 KiB
Python

from mod.mysql import Mysql
import json
import datetime
class News:
#获取全部文章的方法
def content(self):
mysql = Mysql()
sql = "SELECT * From news"
execute = mysql.exeCommit(sql)
if(execute >= 1):
cursor = mysql.cursor
result = cursor.fetchall()
del mysql
data = {'status': 'success', 'msg': '获取文章成功', 'data':result}
return json.dumps(data, cls=CJsonEncoder)
else:
del mysql
data = {'status': 'faild', 'msg': '没有文章' , 'data':None}
return json.dumps(data, cls=CJsonEncoder)
class CJsonEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime.datetime):
return obj.strftime('%Y-%m-%d %H:%M:%S')
elif isinstance(obj, datetime.date):
return obj.strftime("%Y-%m-%d")
else:
return json.JSONEncoder.default(self, obj)
if __name__ == '__main__':
news = News()
result = news.content()
print(result)