python Sql Alchemy 有什么问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2048084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sql Alchemy What is wrong?
提问by kn3l
I got the tutorial
我得到了教程
When I compile got the error message
当我编译得到错误信息
The debugged program raised the exception unhandled NameError
"name 'BoundMetaData' is not defined"
I am use the latest sqlAlchemy .
我正在使用最新的 sqlAlchemy 。
How Could I fixed this?
我怎么能解决这个问题?
After read this I modified to my own for latest version 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
It raise error
它引发错误
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' ()
回答by shashaDenovo
The fix for the tutorial is to just use MetaData
instead of BoundMetaData
. BoundMetaData is deprecated and replaced with MetaData.
本教程的修复方法是仅使用MetaData
而不是 BoundMetaData
. BoundMetaData 已弃用并替换为 MetaData。
And to avoid getting such error in future, Try the officialone instead,
as Nosklo
said.
并避免在未来得到这样的错误,请尝试官员,而不是之一,因为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
"""
回答by nosklo
This tutorial is for SQLAlchemy version 0.2. Since the actual version is 0.5.7, I'd say the tutorial is severely outdated.
本教程适用于 SQLAlchemy 0.2 版。由于实际版本是 0.5.7,我会说教程已经严重过时了。
Try the officialone instead.
试试官方的吧。
EDIT:
编辑:
Now you have a completely different question. You should've asked another question instead of editing this one.
现在你有一个完全不同的问题。你应该问另一个问题而不是编辑这个问题。
Your problem now is that
你现在的问题是
Column('password', String),
Doesn't specify a size for the column.
不指定列的大小。
Try
尝试
Column('password', String(20)),
Instead.
反而。