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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:39:19  来源:igfitidea点击:

Sql Alchemy What is wrong?

pythonmysqlsqlalchemy

提问by kn3l

I got the tutorial

我得到了教程

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

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

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 MetaDatainstead 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 Nosklosaid.

并避免在未来得到这样的错误,请尝试官员,而不是之一,因为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.

反而。

回答by codeape

I believe you need to specify the length of your passwordfield.

我相信您需要指定password字段的长度。

Column('password', String(100))

MySQL does not allow unbounded varchar columns. If you need that, use the sqlalchemy datatype Textinstead.

MySQL 不允许无界 varchar 列。如果需要,请改用 sqlalchemy 数据类型Text