在資料庫中建立資料表
你已經設定好 engine 並初始化了 metadata。現在要定義 census 資料表物件,接著使用上一個練習中的 metadata 與 engine 在資料庫中建立它。要在資料庫裡建立資料表,必須在 metadata 上呼叫 .create_all() 方法,並將 engine 作為引數。
你可以回到這個你曾學過如何建立資料表的[第 4 章練習](https://campus.datacamp.com/courses/introduction-to-relational-databases-in-python/creating-and-manipulating-your-own-databases?ex=2) 參考一下。
本練習屬於課程
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
____