在数据库中创建表
在已经设置好 engine 并初始化元数据后,您现在将定义 census 表对象,然后使用上一练习中的 metadata 和 engine 在数据库中创建它。要在数据库中创建该表,您需要在 metadata 上调用 .create_all() 方法,并将 engine 作为参数传入。
您可以回看这道您学习如何创建表的第 4 章练习。
本练习是课程的一部分
Python 中的数据库入门
练习说明
- 从
sqlalchemy导入Table、Column、String和Integer。 - 定义一个名为
census的表,包含以下列:'state'- String - 长度 30'sex'- String - 长度 1'age'- Integer'pop2000'- Integer'pop2008'- Integer
- 使用
metadata和engine在数据库中创建该表。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Import Table, Column, String, and Integer
# Build a census table: census
census = Table('census', metadata,
Column('state', ____),
Column(____, ____),
____,
____,
____)
# Create the table in the database
____