the first commit
This commit is contained in:
parent
f7713c3979
commit
543f616575
49
mod/mysql.py
Normal file
49
mod/mysql.py
Normal file
@ -0,0 +1,49 @@
|
||||
# -*- coding: UTF-8 -*-
|
||||
import pymysql
|
||||
|
||||
class Mysql(object):
|
||||
|
||||
def __init__(self):
|
||||
|
||||
#配置mysql数据库信息
|
||||
config = {
|
||||
'host': '127.0.0.1',
|
||||
'port': 3306,
|
||||
'user': 'root',
|
||||
'passwd': '123456',
|
||||
'db': 'news',
|
||||
'charset': 'utf8'
|
||||
}
|
||||
|
||||
self.host = config['host']
|
||||
self.username = config['user']
|
||||
self.password = config['passwd']
|
||||
self.port = config['port']
|
||||
self.db = config['db']
|
||||
self.con = None
|
||||
self.cursor = None
|
||||
|
||||
try:
|
||||
self.conn = pymysql.connect(**config)
|
||||
# 所有的查询,都在连接 con 的一个模块 cursor 上面运行的
|
||||
self.cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursor)
|
||||
except:
|
||||
print("DataBase connect error,please check the db config.")
|
||||
|
||||
def exeCommit(self, sql=''):
|
||||
"""执行数据库sql语句,针对更新,删除,事务等操作失败时回滚
|
||||
"""
|
||||
try:
|
||||
execute = self.cursor.execute(sql)
|
||||
self.conn.commit()
|
||||
return execute
|
||||
except pymysql.Error as e:
|
||||
self.conn.rollback()
|
||||
error = 'MySQL execute failed! ERROR (%s): %s' % (e.args[0], e.args[1])
|
||||
print("error:", error)
|
||||
return error
|
||||
|
||||
def __del__(self):
|
||||
# 关闭cursor对象
|
||||
self.cursor.close()
|
||||
self.conn.close()
|
37
mod/news.py
Normal file
37
mod/news.py
Normal file
@ -0,0 +1,37 @@
|
||||
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)
|
7
requirements.txt
Normal file
7
requirements.txt
Normal file
@ -0,0 +1,7 @@
|
||||
cheroot==8.2.1
|
||||
jaraco.functools==3.0.0
|
||||
Markdown==3.1.1
|
||||
more-itertools==8.0.2
|
||||
PyMySQL==0.9.3
|
||||
six==1.13.0
|
||||
web.py==0.40
|
24
run.py
Normal file
24
run.py
Normal file
@ -0,0 +1,24 @@
|
||||
# -*- coding: UTF-8 -*-
|
||||
import web
|
||||
from mod.news import News
|
||||
|
||||
urls = (
|
||||
'/news','api',
|
||||
)
|
||||
|
||||
# render = web.template.render('templates')
|
||||
app = web.application(urls, globals())
|
||||
|
||||
|
||||
#api的方法
|
||||
class api:
|
||||
def GET(self):
|
||||
news = News()
|
||||
return news.content()
|
||||
def POST(self):
|
||||
news = News()
|
||||
return news.content()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
BIN
static/1.jpg
Normal file
BIN
static/1.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 250 KiB |
BIN
static/2.jpg
Normal file
BIN
static/2.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 11 KiB |
Loading…
Reference in New Issue
Block a user