填充数据库

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...