-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathaddress_book_sql.py
More file actions
executable file
·42 lines (30 loc) · 988 Bytes
/
address_book_sql.py
File metadata and controls
executable file
·42 lines (30 loc) · 988 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python
import os
import sqlite3
import sys
from address_book_model import create_sample
SCHEMA_FILE = "address_book_ddl.sql"
DB_FILE = "address.sqlite3"
if os.path.isfile(DB_FILE):
print "%s exists, exiting" % DB_FILE
sys.exit(-1)
conn = sqlite3.connect(DB_FILE)
cursor = conn.cursor()
def create_schema():
with open(SCHEMA_FILE, 'rt') as f:
schema = f.read()
conn.executescript(schema)
create_schema()
address_book = create_sample()
for person in address_book.people:
insert_statement = "INSERT INTO Person(first_name, last_name, middle_name, cell_phone, email) VALUES (?,?,?,?,?)"
row = ( person.first_name,
person.last_name,
person.middle_name,
person.cell_phone,
person.email)
cursor.execute(insert_statement, row)
print cursor.rowcount
conn.commit()
# TODO: also insert Addresses, Businesses, AddressBook, and relationships between them.
conn.close()