import sqlite3
import hashlib

conn = sqlite3.connect('myApp.db')
cur = conn.cursor()

print ("Opened database successfully")

conn.execute('''CREATE TABLE USERS
	(id		INTEGER PRIMARY KEY AUTOINCREMENT    NOT NULL,
	login		TEXT,
	password	TEXT,
	name		TEXT
	);''')


conn.execute('''CREATE TABLE TOKEN
	(id		INTEGER PRIMARY KEY AUTOINCREMENT    NOT NULL,
	token		TEXT,
	devid		TEXT,
	login		TEXT,
	expire		INTEGER
	);''')


print ("Tables created successfully")

#############################
# Insert Data into table USERS
#############################

users = [
        {'id':1, 'name': 'Administrator', 'login':'admin', 'password': 'admin'},
	{'id':2, 'name': 'User', 'login':'user', 'password': '123'}
]

for item in users:
	password_hash = hashlib.md5(item['password'].encode()).hexdigest()
	SQL = 'INSERT INTO USERS (id, name, login, password) VALUES({0}, "{1}", "{2}", "{3}")'.format(item['id'], item['name'], item['login'], password_hash)
	cur.execute(SQL)

print ("Table 'USERS' is done")

conn.commit()

print ("Commit is done")

conn.close()

print ("Database is closed")
print ("Good luck!")
