forked from seven-liu/python_study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite3_create_schema.py
More file actions
39 lines (25 loc) · 1011 Bytes
/
sqlite3_create_schema.py
File metadata and controls
39 lines (25 loc) · 1011 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
#encoding:utf-8
import os
import sqlite3
db_filename='todo.db'
schema_filename='todo_schema.sql'
db_is_new=not os.path.exists(db_filename)
with sqlite3.connect(db_filename) as conn:
if db_is_new:
print "creating schema"
with open(schema_filename,'rt') as f:
schema=f.read()
conn.executescript(schema)
print "Inserting initial data"
conn.executescript("""
insert into project (name,description,deadline)
values('pymotw','python module of the week','2010-11-01');
insert into task (details,status,deadline,project)
values('write about select','done','2010-10-02','pymotw');
insert into task (details,status,deadline,project)
values('write about random','waiting','2010-10-10','pymotw');
insert into task (details,status,deadline,project)
values('write about sqlite3','active','2010-10-18','pymotw');
""")
else:
print 'Database exists,assume schema does,too.'