from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATEBASE_URL'] = 'mysql://root:@127.0.0.1/net_news?charset=utf8' db = SQLAlchemy(app) class News(db.Model): __tablename__ = 'news' ID = db.Column(db.Integer, primary_key = True) #如果数据库中的表格id 标签为“XXid” 则需要在Column()里标明 title = db.Column(db.String(200), nullable = False) content = db.Column(db.String(2000), nullable = False) types = db.Column(db.String(10), nullable = False) image = db.Column(db.String(300)) author = db.Column(db.String(20)) view_count = db.Column(db.Integer) crate_time = db.Column(db.DateTime) is_valid = db.Column(db.Boolean) def __repr__(self): return '<News %/r>' % self.title def main(): db.create_all() if __name__ == '__main__': main()
老师,运行上述代码,出现如下错误,感觉错在创建表 db.create_all()这一段,那到底该如何很好的去实现(不太习惯在cmd中使用)
C:\Users\560\Anaconda3\lib\site-packages\flask_sqlalchemy\__init__.py:774: UserWarning: Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:///:memory:". 'Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. '
C:\Users\560\Anaconda3\lib\site-packages\flask_sqlalchemy\__init__.py:794: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning. 'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
还有就是这个Flask-SQLAlchemy的中文说明书用的是from flask.ext.sqlalchemy import SQLAlchemy 这与flask_sqlachemy有什么区别么
http://docs.jinkan.org/docs/flask-sqlalchemy/quickstart.html#id2
一次实战同时掌握Python操作MySQL,MongoDB,Redis 三大数据库使用技巧
了解课程1.2k 13
1.1k 12
3.0k 12
1.6k 11
5.7k 10