name 'BoundMetaData' is not defined
Question:
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 .
How Could I fixed this?
After read this I modified to my own for latest version 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 ' ntPRIMARY KEY (user_id)n)' at line 5") 'nCREATE TABLE users (ntuser_id INTEGER NOT NULL AUTO_INCREMENT, ntname VARCHAR(40), ntage INTEGER, ntpassword VARCHAR, ntPRIMARY KEY (user_id)n)nn' ()
Answers:
This tutorial is for SQLAlchemy version 0.2. Since the actual version is 0.5.7, I’d say the tutorial is severely outdated.
Try the official one 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.
I believe you need to specify the length of your password
field.
Column('password', String(100))
MySQL does not allow unbounded varchar columns. If you need that, use the sqlalchemy datatype Text
instead.
The fix for the tutorial is to just use MetaData
instead of BoundMetaData
. BoundMetaData is deprecated and replaced with MetaData.
And to avoid getting such error in future, Try the official one instead,
as Nosklo
said.
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
"""
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 .
How Could I fixed this?
After read this I modified to my own for latest version 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 ' ntPRIMARY KEY (user_id)n)' at line 5") 'nCREATE TABLE users (ntuser_id INTEGER NOT NULL AUTO_INCREMENT, ntname VARCHAR(40), ntage INTEGER, ntpassword VARCHAR, ntPRIMARY KEY (user_id)n)nn' ()
This tutorial is for SQLAlchemy version 0.2. Since the actual version is 0.5.7, I’d say the tutorial is severely outdated.
Try the official one 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.
I believe you need to specify the length of your password
field.
Column('password', String(100))
MySQL does not allow unbounded varchar columns. If you need that, use the sqlalchemy datatype Text
instead.
The fix for the tutorial is to just use MetaData
instead of BoundMetaData
. BoundMetaData is deprecated and replaced with MetaData.
And to avoid getting such error in future, Try the official one instead,
as Nosklo
said.
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
"""