# http://flask-wtf.readthedocs.io/en/stable/install.html#distribute-pip
# pip install flask-sqlalchemy
from flask import Flask,render_template,request,flash
from flask_wtf import FlaskForm
from wtforms import SubmitField,StringField,PasswordField
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
报错
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:[email protected]/xiaoshuo'
正确的方式:
# app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:[email protected]/xiaoshuo'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
/www/env/movie/bin/python3.5 /www/flask_wtf_demo/flask_wtf_demo.py
Traceback (most recent call last):
File "/www/flask_wtf_demo/flask_wtf_demo.py", line 86, in <module>
db.create_all()
File "/www/env/movie/lib/python3.5/site-packages/flask_sqlalchemy/__init__.py", line 963, in create_all
self._execute_for_all_tables(app, bind, 'create_all')
File "/www/env/movie/lib/python3.5/site-packages/flask_sqlalchemy/__init__.py", line 955, in _execute_for_all_tables
op(bind=self.get_engine(app, bind), **extra)
File "/www/env/movie/lib/python3.5/site-packages/flask_sqlalchemy/__init__.py", line 896, in get_engine
return connector.get_engine()
File "/www/env/movie/lib/python3.5/site-packages/flask_sqlalchemy/__init__.py", line 559, in get_engine
self._engine = rv = sqlalchemy.create_engine(info, **options)
File "/www/env/movie/lib/python3.5/site-packages/sqlalchemy/engine/__init__.py", line 424, in create_engine
return strategy.create(*args, **kwargs)
File "/www/env/movie/lib/python3.5/site-packages/sqlalchemy/engine/strategies.py", line 81, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File "/www/env/movie/lib/python3.5/site-packages/sqlalchemy/dialects/mysql/mysqldb.py", line 102, in dbapi
return __import__('MySQLdb')
ImportError: No module named 'MySQLdb'
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:[email protected]/xiaoshuo'
改成以下方式就正确了
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:[email protected]/xiaoshuo'
参考:
http://docs.sqlalchemy.org/en/rel_1_0/dialects/mysql.html#module-sqlalchemy.dialects.mysql.mysqldb
MySQL-Python¶
Support for the MySQL database via the MySQL-Python driver.
DBAPI
Documentation and download information (if applicable) for MySQL-Python is available at: http://sourceforge.net/projects/mysql-python
Connecting
Connect String:
mysql+mysqldb://<user>:<password>@<host>[:<port>]/<dbname>
Unicode
Please see Unicode for current recommendations on unicode handling.
Py3K Support
Currently, MySQLdb only runs on Python 2 and development has been stopped. mysqlclient is fork of MySQLdb and provides Python 3 support as well as some bugfixes.
Using MySQLdb with Google Cloud SQL
Google Cloud SQL now recommends use of the MySQLdb dialect. Connect using a URL like the following:
mysql+mysqldb://root@/<dbname>?unix_socket=/cloudsql/<projectid>:<instancename>
pymysql
Support for the MySQL database via the PyMySQL driver.
DBAPI
Documentation and download information (if applicable) for PyMySQL is available at: http://www.pymysql.org/
Connecting
Connect String:
mysql+pymysql://<username>:<password>@<host>/<dbname>[?<options>]
Unicode
Please see Unicode for current recommendations on unicode handling.
MySQL-Python Compatibility
The pymysql DBAPI is a pure Python port of the MySQL-python (MySQLdb) driver, and targets 100% compatibility. Most behavioral notes for MySQL-python apply to the pymysql driver as well.
MySQL-Connector
Support for the MySQL database via the MySQL Connector/Python driver.
DBAPI
Documentation and download information (if applicable) for MySQL Connector/Python is available at: http://dev.mysql.com/downloads/connector/python/
Connecting
Connect String:
mysql+mysqlconnector://<user>:<password>@<host>[:<port>]/<dbname>
Unicode
Please see Unicode for current recommendations on unicode handling.
cymysql
Support for the MySQL database via the CyMySQL driver.
DBAPI
Documentation and download information (if applicable) for CyMySQL is available at: https://github.com/nakagami/CyMySQL
Connecting
Connect String:
mysql+cymysql://<username>:<password>@<host>/<dbname>[?<options>]
OurSQL
Support for the MySQL database via the OurSQL driver.
DBAPI
Documentation and download information (if applicable) for OurSQL is available at: http://packages.python.org/oursql/
Connecting
Connect String:
mysql+oursql://<user>:<password>@<host>[:<port>]/<dbname>
Unicode
Please see Unicode for current recommendations on unicode handling.
Google App Engine
Support for the MySQL database via the Google Cloud SQL driver.
This dialect is based primarily on the mysql.mysqldb
dialect with minimal changes.
New in version 0.7.8.
Deprecated since version 1.0: This dialect is no longer necessary for Google Cloud SQL; the MySQLdb dialect can be used directly. Cloud SQL now recommends creating connections via the mysql dialect using the URL format
mysql+mysqldb://root@/<dbname>?unix_socket=/cloudsql/<projectid>:<instancename>
更多内容请关注我的订阅号: