亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

python - flask-sqlachemy使用mysql,出現(xiàn)錯誤Internal Error
伊謝爾倫
伊謝爾倫 2017-04-17 17:38:01
0
3
668

最近在學(xué)習(xí)Flask Web開發(fā)這本書
用flask-sqlachemy連接到mysql的時候,參考書中提供的URI,把sqlite的改成了mysql的
這是config文件

import os

class Config:
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'hard to guess string'
    FLASKY_ADMIN = os.environ.get('FLASKY_ADMIN')
    SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:7123@localhost/database'
    SQLALCHEMY_COMMIT_ON_TEARDOWN = True

    @staticmethod
    def init_app(app):
        pass

config = Config()請輸入代碼

這是__init__文件

from flask import Flask
from flask.ext.bootstrap import Bootstrap
from flask.ext.moment import Moment
from flask.ext.sqlalchemy import SQLAlchemy
from config import config
from flask.ext.login import LoginManager
from flask.ext.pagedown import PageDown
import pymysql

pymysql.install_as_MySQLdb()
bootstrap = Bootstrap()
moment = Moment()
db = SQLAlchemy()
pagedown = PageDown()

login_manager = LoginManager()
login_manager.session_protection = 'strong'
login_manager.login_view = 'auth.login'


def create_app(config):
    app = Flask(__name__)
    app.config.from_object(config)
    config.init_app(app)

    bootstrap.init_app(app)
    moment.init_app(app)
    db.init_app(app)
    login_manager.init_app(app)
    pagedown.init_app(app)

    from .main import main as main_blueprint
    app.register_blueprint(main_blueprint)

    from .auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint, url_prefix='/auth')

    return app

@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))

這是啟動文件manage

#!/usr/bin/env python
import os
from app import create_app, db
from app.models import User, Role
from flask.ext.script import Manager, Shell
from flask.ext.migrate import Migrate, MigrateCommand
from config import config
from flask.ext.sqlalchemy import SQLAlchemy


app = create_app(config)
manager = Manager(app)
migrate = Migrate(app, db)


def make_shell_context():
    return dict(app=app, db=db, User=User, Role=Role)
manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command('db', MigrateCommand)


@manager.command
def test():
    """Run the unit tests."""
    import unittest
    tests = unittest.TestLoader().discover('tests')
    unittest.TextTestRunner(verbosity=2).run(tests)


if __name__ == '__main__':
    manager.run()

在使用python manage.py shell后,使用db.create_all()
拋出錯誤

raise InternalError(errno, errorvalue)
sqlalchemy.exc.InternalError: (InternalError) (1049, "Unknown database 'database'") None None

找了半天,開始的時候就是直接改的config里的SQLALCHEMY_DATABASE_URI,會出現(xiàn)ImportError: No module named 'MySQLdb',我想是因?yàn)闆]有跟MySQL接起來,按照https://www.zhihu.com/question/29719547這個帖子里面講的,用了個pymysql來做,結(jié)果就是上面那個樣子,求教求教
查了挺多資料,似乎還有另外一條路子,就是放棄ORM,用sql語句查詢,用engine建立數(shù)據(jù)庫連接?
謝謝~

伊謝爾倫
伊謝爾倫

小伙看你根骨奇佳,潛力無限,來學(xué)PHP伐。

reply all(3)
迷茫

I have encountered the same problem before, so I will share it now.
First of all, sqlalchemy requires MySQLdb to connect to MySQL. This library generally cannot be installed with pip or easy_install and requires many dependencies.

linux:

sudo apt-get install python-mysqldb (ubuntu)
yum install MySQL-python (Linux Fedora, CentOS...)

windows:

MySQL-python needs to be installed
The link is here https://pypi.python.org/pypi/MySQL-python/1.2.5

After installation, open python and import MySQL to see if there will be an error. If not, then it is installed.

劉奇

The problem is solved. First of all, if you want to use MySQL, you must use PyMySQL or as @island said, MySQLdb. There is a problem with MySQLdb’s support for python. The link to the answer above supports py2.7. I remember seeing one. According to the information, this support is only available in py3.4, but not in py3.5. I have not tried this. Friends who encounter difficulties can try PyMySQL. Remember to import pymysql and pymysql.install_as_MySQLdb(), and the URI is also Change accordingly
Then, as @NSDont said, create_all() can only create tables. You also need to use MySQL to create a database yourself (the name of the database cannot be stupidly called DATABASE).
In the last URI, if it is a local database, the server is usually localhost:3306. Pay attention to this place
This is my first question, thank you two upstairs~

PHPzhong

Well, this error seems to be that there is no database.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template