|
1
|
|
|
#This file create a SQLite local database for CNBP requirement. |
|
2
|
|
|
#Creation: 2018-07-11T160228EST |
|
3
|
|
|
#Author: Yang Ding |
|
4
|
|
|
|
|
5
|
|
|
import sys |
|
6
|
|
|
import os |
|
7
|
|
|
import sqlite3 |
|
8
|
|
|
import logging |
|
9
|
|
|
from pathlib import Path |
|
10
|
|
|
from LocalDB_schema import * |
|
11
|
|
|
import argparse |
|
12
|
|
|
import getpass |
|
13
|
|
|
|
|
14
|
|
|
logging.basicConfig(stream=sys.stdout, level=logging.INFO) |
|
15
|
|
|
logger = logging.getLogger('LocalDBCreate') |
|
16
|
|
|
|
|
17
|
|
|
def LocalDBCreate(PathString, TableName, KeyFieldString, ColumnsNameTypeList): |
|
18
|
|
|
""" |
|
19
|
|
|
Create the local database based on sceham. |
|
20
|
|
|
:param PathString: |
|
21
|
|
|
:param TableName: |
|
22
|
|
|
:param KeyFieldString: |
|
23
|
|
|
:param ColumnsNameTypeList: |
|
24
|
|
|
:return: |
|
25
|
|
|
""" |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
# if SQL already exist, quit script. |
|
29
|
|
|
SQLPath = Path(PathString) |
|
30
|
|
|
|
|
31
|
|
|
# check if path is a fiela nd exist. |
|
32
|
|
|
if SQLPath.is_file(): |
|
33
|
|
|
logger.info('SQLite database file already exist. Not gonna mess with it!') |
|
34
|
|
|
return False |
|
35
|
|
|
'''Delete current database! During testing only''' |
|
36
|
|
|
'''os.remove(sqliteFile) |
|
37
|
|
|
logger.info('DEBUG: database file already exist. Deleted it!')''' |
|
38
|
|
|
|
|
39
|
|
|
#Create the PRIMARY KEY column. |
|
40
|
|
|
KeyFieldType = CNBP_schema_keyfield_type # column data type |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
#Try to connect the database to start the process: |
|
43
|
|
|
|
|
44
|
|
|
try: |
|
45
|
|
|
# Create on Connecting to the database file |
|
46
|
|
|
ConnectedDatabase = sqlite3.connect(PathString) |
|
47
|
|
|
|
|
48
|
|
|
c = ConnectedDatabase.cursor() |
|
49
|
|
|
|
|
50
|
|
|
logger.info('Creating PRIMARY KEY DBKEY column in database.') |
|
51
|
|
|
|
|
52
|
|
|
# Creating a new SQLite table with DBKey column (inspired by: https://sebastianraschka.com/Articles/2014_sqlite_in_python_tutorial.html) |
|
53
|
|
|
c.execute('CREATE TABLE {tn} ({nf} {ft} PRIMARY KEY)'.format(tn=TableName, nf=KeyFieldString, ft=KeyFieldType)) |
|
54
|
|
|
|
|
55
|
|
|
logger.info('PRIMARY KEY DBKEY column successfully created in database.') |
|
56
|
|
|
|
|
57
|
|
|
logger.info('Creating secondary columns in database.') |
|
58
|
|
|
|
|
59
|
|
|
# Adding accessory columns via a loop |
|
60
|
|
|
for column in ColumnsNameTypeList: |
|
61
|
|
|
if (column[1] != "TEXT" and |
|
62
|
|
|
column[1] != "REAL" and |
|
63
|
|
|
column[1] != "BLOB" and |
|
64
|
|
|
#column[1] != "NULL" and |
|
65
|
|
|
column[1] != "INTEGER"): |
|
66
|
|
|
continue # skip iteration is the data type is not specified properly. |
|
67
|
|
|
else: |
|
68
|
|
|
c.execute("ALTER TABLE {tn} ADD COLUMN '{cn}' {ct}".format(tn=TableName, cn=column[0], ct=column[1])) |
|
69
|
|
|
|
|
70
|
|
|
logger.info('Secondary columns created in database.') |
|
71
|
|
|
|
|
72
|
|
|
# Committing changes and closing the connection to the database file |
|
73
|
|
|
ConnectedDatabase.commit() |
|
74
|
|
|
ConnectedDatabase.close() |
|
75
|
|
|
except: |
|
76
|
|
|
logger.info('SQLite database creation/update issue, suspect schema non-compliant SQLite database. Did you corrupt this SQLite database somehow?') |
|
77
|
|
|
raise IOError |
|
78
|
|
|
return True |