gpt4 book ai didi

python - "NameError: name ' sqlalchemy中的 float ' is not defined"

转载 作者:太空狗 更新时间:2023-10-30 00:29:58 26 4
gpt4 key购买 nike

怎么会出现错误

Traceback (most recent call last):
File "stx_sql.py", line 19, in<module>
Column('value', Float),
NameError: name 'Float' is not defined

通过运行代码

from sqlalchemy import create_engine
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey

engine = create_engine('sqlite:///tttx.sqlite', echo=True)
#engine = create_engine('sqlite:///:memory:', echo=True)

metadata = MetaData()
users_table = Table('users', metadata,
Column('id', Integer, primary_key=True),
Column('name', String(50), nullable=False),
Column('fullname', String(50), nullable=False),
Column('password', String(70), nullable=False)
)
points_table = Table('points', metadata,
Column('id', Integer, primary_key=True),
Column('name', String(50), unique=True, nullable=False),
Column('description', String(150)),
Column('type', Integer),
Column('value', Float),
Column('refreshtime', Float),
Column('lastupdate', Float)
)
types_table = Table('types', metadata,
Column('id', Integer, primary_key=True),
Column('name', String(50), unique=True, nullable=False),
Column('description', String(150))
)
metadata.create_all(engine)

然后我在 win 上使用 Python 2.7.5(默认,2013 年 5 月 15 日,22:43:36)[MSC v.1500 32 位(英特尔)]32 和 sqlalchemy 0.8.2然后我使用 Python 2.7.4(默认,2013 年 9 月 26 日,03:20:56)[GCC 4.7.3] 在 linux2 和 sqlalchemy 0.7.9 上

附注还有

NameError: name 'Numeric' is not defined

最佳答案

您需要导入该对象,但您尚未在代码中这样做。

只需将 Float 添加到您现有的 from sqlalchemy import 行:

from sqlalchemy import Table, Column, Float, Integer, String, MetaData, ForeignKey

这同样适用于Numeric;您需要显式导入它,它不是 Python 内置的。

关于python - "NameError: name ' sqlalchemy中的 float ' is not defined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19296226/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com