填入資料庫

Python 資料庫入門

Jason Myers

Co-Author of Essential SQLAlchemy and Software Engineer

第 2 部分:填入資料庫

  • 將 CSV 檔載入為數值清單
values_list = []
for row in csv_reader:
    data = {'state': row[0], 'sex': row[1], 'age': row[2],
            'pop2000': row[3], 'pop2008': row[4]}
    values_list.append(data)
Python 資料庫入門

第 2 部分:填入資料庫

  • 將數值清單插入 census 資料表
from sqlalchemy import insert

stmt = insert(employees)
result_proxy = connection.execute(stmt, values_list) print(result_proxy.rowcount)
2
Python 資料庫入門

一起來練習吧!

Python 資料庫入門

Preparing Video For Download...