Created
June 30, 2014 07:19
-
-
Save migonzalvar/508acde3ad41a8d3379a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
import yaml | |
DATABASE = 'inventory.db' | |
with open('schema.yaml') as f: | |
schema = yaml.load(f) | |
def create_table(table, *fields): | |
"""Define SQL statement to create a table. | |
>>> create_table('t', 'c1', 'c2') | |
'CREATE TABLE t (c1, c2)' | |
""" | |
field_list = ', '.join(fields) | |
tpl = 'CREATE TABLE {table} ({field_list})' | |
sql = tpl.format(table=table, field_list=field_list) | |
return sql | |
def init_db(schema): | |
"""Create schema.""" | |
conn = sqlite3.connect(DATABASE) | |
cursor = conn.cursor() | |
for table in schema: | |
create_table_stmt = create_table(table, *schema[table]) | |
cursor.execute(create_table_stmt) | |
conn.commit() | |
def test_init_db(): | |
global DATABASE | |
DATABASE = 'test.db' | |
schema = {'t': ['c1', 'c2']} | |
init_db(schema) | |
conn = sqlite3.connect(DATABASE) | |
cursor = conn.cursor() | |
result = cursor.execute( | |
'SELECT name FROM sqlite_master WHERE type = "table" and name = ?', | |
['t', ] | |
) | |
result = list(result) | |
print(result) | |
assert result[0][0] == 't' | |
def user_add(login, password, profile='Read'): | |
# Prepare/data: | |
# - login valid (regexp) [a-zA-Z0-9] | |
# - hash password sha1 | |
pass | |
# Insert in database | |
pass | |
# Return pk | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment