使用sqlalchemy创建表

时间:2012-04-25 17:28:09

标签: python sqlalchemy

在使用sqlalchemy创建表格时,我是否遗漏了一些内容?

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

engine = create_engine('postgresql+psycopg2://postgres:password#@localhost:5432/LabData',echo=True)

metadata=MetaData(bind=engine)
main_table=('sample',metadata,
            Column('LIN',String(10),primary_key=True),
            Column('material_type',String(20),nullable=False),
            Column('source',String(20),nullable=False),
            Column('material_description',String(100)),
            Column('quantity',Integer),
            Column('location',String(2)),
            Column('received_by',String(20)),
            Column('received_date',DateTime,nullable=False),
            )

metadata.create_all()

python控制台上的输出是:

2012-04-25 13:13:42,734 INFO sqlalchemy.engine.base.Engine select version()
2012-04-25 13:13:42,796 INFO sqlalchemy.engine.base.Engine {}
2012-04-25 13:13:42,796 INFO sqlalchemy.engine.base.Engine select current_schema()
2012-04-25 13:13:42,796 INFO sqlalchemy.engine.base.Engine {}

当我尝试将数据添加到表中时,它说的是错误的错误。

1 个答案:

答案 0 :(得分:3)

可能的拼写错误?

main_table=('sample',metadata,...

也许应该是

main_table=Table('sample',metadata,...
#          ^^^^^