Advertisement
rfmonk

sqlite3_create_schema.py

Jan 29th, 2014
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3.  
  4. import os
  5. import sqlite3
  6.  
  7. db_filename = 'todo.db'
  8. schema_filename = 'todo_schema.sql'
  9.  
  10. db_is_new = not os.path.exists(db_filename)
  11.  
  12. with sqlite3.connect(db_filename) as conn:
  13.     if db_is_new:
  14.         print 'Creating schema'
  15.         with open(schema_filename, 'rt') as f:
  16.             schema = f.read()
  17.         conn.executescript(schema)
  18.  
  19.         print 'Inserting initial data'
  20.  
  21.         conn.executescript("""
  22.        insert into project (name, description, deadline)
  23.        values ('pymotw', 'Python Module of the Week', '2010-11-01');
  24.  
  25.        insert into task (details, status, deadline, project)
  26.        values ('write about select', 'done', '2010-10-03', 'pymotw');
  27.  
  28.        insert into task (details, status, deadline, project)
  29.        values ('write about select', 'done', '2010-10-10', 'pymotw');
  30.  
  31.        insert into task (details, status, deadline, project)
  32.        values ('write about select', 'done', '2010-10-17', 'pymotw');
  33.  
  34.        """)
  35.  
  36.     else:
  37.         print 'Database exists, assume schema does, too.'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement