python - 名称 'BoundMetaData' 未定义

标签 python sqlalchemy

我有教程

http://www.rmunn.com/sqlalchemy-tutorial/tutorial.html

当我编译得到错误信息

The debugged program raised the exception unhandled NameError
"name 'BoundMetaData' is not defined"

我正在使用最新的 sqlAlchemy。

我该如何解决这个问题?

阅读后我修改为我自己的最新版本的sqlAlchemy:

from sqlalchemy import *
engine = create_engine('mysql://root:mypassword@localhost/mysql')
metadata = MetaData()
users = Table('users', metadata,
    Column('user_id', Integer, primary_key=True),
    Column('name', String(40)),
    Column('age', Integer),
    Column('password', String),
)
metadata.create_all(engine) 
i = users.insert()
i.execute(name='Mary', age=30, password='secret')
i.execute({'name': 'John', 'age': 42},
          {'name': 'Susan', 'age': 57},
          {'name': 'Carl', 'age': 33})
s = users.select()
rs = s.execute()
row = rs.fetchone()
print 'Id:', row[0]
print 'Name:', row['name']
print 'Age:', row.age
print 'Password:', row[users.c.password]
for row in rs:
    print row.name, 'is', row.age, 'years old

它引发错误

 raise exc.DBAPIError.instance(statement, parameters, e, connection_invalidated=is_disconnect)
sqlalchemy.exc.ProgrammingError: (ProgrammingError) (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' \n\tPRIMARY KEY (user_id)\n)' at line 5") '\nCREATE TABLE users (\n\tuser_id INTEGER NOT NULL AUTO_INCREMENT, \n\tname VARCHAR(40), \n\tage INTEGER, \n\tpassword VARCHAR, \n\tPRIMARY KEY (user_id)\n)\n\n' ()

最佳答案

本教程的修复是仅使用 MetaData 而不是 BoundMetaData。 BoundMetaData 已弃用并替换为 MetaData。

为了避免将来出现此类错误,请尝试 official一个代替, 正如 Nosklo 所说。

from sqlalchemy import *

db = create_engine('sqlite:///tutorial.db')

db.echo = False  # Try changing this to True and see what happens

metadata = MetaData(db)

"""
  Continue with the rest of your Python code
"""

关于python - 名称 'BoundMetaData' 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2048084/

相关文章:

python - bundle (只是--onefile)

python - Storm 或 SQLAlchemy ORM 是否允许从现有数据库创建模式?

python - 使用 SQLAlchemy 获取页面的特定数量的结果

python - SQLAlchemy InvalidRequestError : failed to locate name happens only on gunicorn

python - 修改数据框中的值

python - 获取当前目录中所有子目录的列表

python - 如何确定某个值是否在双端队列中?

python - 将python中的x轴替换为y轴

python - 在SQLAlchemy中,如何根据月份调用不同的数据库表

python - 在 AWS Lambda 中使用 Python SQLAlchemy 是个好主意吗?