我有一个现有的 mysql 数据库,我正在尝试创建一个 API 来访问它,但我收到了 404 错误。我有两个版本的代码不起作用。
有人可以指出我正确的方向吗?
我正在使用的网址:http: //127.0.0.1 :5000/api/email
v1:
from flask import Flask
from flask.ext.restless import APIManager
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://pathtodb/emails'
db = SQLAlchemy(app)
class Email(db.Model):
__tablename__ = 'emails'
id = db.Column(db.Integer, primary_key=True)
status = db.Column(db.String(20))
email = db.Column(db.String())
def __init__(self, status, email):
self.status = status
self.email = email
api_manager = APIManager(app, flask_sqlalchemy_db=db)
api_manager.create_api(Email, methods=['GET', 'POST', 'DELETE', 'PUT'])
if __name__ == "__main__":
app.run(debug=True)
v2:
from flask import Flask
from flask.ext.restless import APIManager
from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
app = Flask(__name__)
Base = declarative_base()
engine = create_engine('mysql+pymysql://pathtodb/emails', echo=True)
metadata = MetaData(bind=engine)
class Email(Base):
__tablename__ = Table('emails', metadata, autoload=True)
id = Column(Integer, primary_key=True)
status = Column(String(20))
email = Column(String())
api_manager = APIManager(app, flask_sqlalchemy_db=engine)
api_manager.create_api(Email, methods=['GET', 'POST', 'DELETE', 'PUT'])
if __name__ == "__main__":
app.run(debug=True)
我正在使用的网址:http: //127.0.0.1 :5000/api/email