Populating the database

Introduction to Databases in Python

Jason Myers

Co-Author of Essential SQLAlchemy and Software Engineer

Part 2: populating the database

  • Load a CSV file into a values list
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)
Introduction to Databases in Python

Part 2: Populating the Database

  • Insert the values list into the census table
from sqlalchemy import insert

stmt = insert(employees)
result_proxy = connection.execute(stmt, values_list) print(result_proxy.rowcount)
2
Introduction to Databases in Python

Let's practice!

Introduction to Databases in Python

Preparing Video For Download...