无法连接到我在flask应用程序中的现有mysql数据库

时间:2017-03-14 21:15:39

标签: python flask sqlalchemy

engine = create_engine(engine_URL)
  File "build/bdist.macosx-10.12-intel/egg/sqlalchemy/engine/__init__.py", line 387, in create_engine
  File "build/bdist.macosx-10.12-intel/egg/sqlalchemy/engine/strategies.py", line 56, in create
  File "build/bdist.macosx-10.12-intel/egg/sqlalchemy/engine/url.py", line 139, in _get_entrypoint
  File "build/bdist.macosx-10.12-intel/egg/sqlalchemy/util/langhelpers.py", line 195, in load
  File "build/bdist.macosx-10.12-intel/egg/sqlalchemy/dialects/__init__.py", line 30, in _auto_fn
ValueError: too many values to unpack

我的代码

import os
import sys
from sqlalchemy import Column, ForeignKey, Integer, String, REAL, DATE, Boolean
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy.engine.url import URL
from sqlalchemy import create_engine

engine_URL = URL('mysql://root:password@0.0.0.0:5000/dbase.sql')
engine = create_engine(engine_URL)
Base = declarative_base()
Base.metadata.reflect(engine)

class User(Base):
    __table__ = Base.metadata.tables['user']

if __name__ == '__main__':
    from sqlalchemy.orm import scoped_session, sessionmaker, Query
    db_session = scoped_session(sessionmaker(bind=engine))
    for item in db_session.query(User.name):
        print item

1 个答案:

答案 0 :(得分:0)

您将连接网址字符串作为drivername传递给URL。生成的URL是

In [3]: engine_URL = URL('mysql://root:password@0.0.0.0:5000/dbase.sql')

In [4]: engine_URL
Out[4]: mysql://root:password@0.0.0.0:5000/dbase.sql://

如果要使用URL的实例,并从连接字符串创建它,请使用make_url

In [8]: engine_URL = make_url('mysql://root:password@0.0.0.0:5000/dbase.sql')

In [9]: engine_URL
Out[9]: mysql://root:***@0.0.0.0:5000/dbase.sql

您也可以按原样将连接网址字符串传递给create_engine()