data-manipulation/dm.py

68 lines
2.3 KiB
Python
Raw Normal View History

2017-03-12 15:02:21 +03:00
# -*- coding: utf-8 -*-
2017-03-13 16:50:33 +03:00
import json
def createTables(tbl_list):
2017-03-14 17:01:30 +03:00
global tbl_descr_list
2017-03-13 16:50:33 +03:00
i = 0
tbl_names_list = []
2017-03-14 17:01:30 +03:00
tbl_descr_list = []
2017-03-13 16:50:33 +03:00
while i < len(tbl_list):
2017-03-14 17:01:30 +03:00
one_Table_descr = []
2017-03-13 16:50:33 +03:00
tbl_descr = tbl_list[i]["tableDescription"]
tbl_name = tbl_list[i]["tableName"]
field_list = tbl_list[i]["fieldList"][i].keys()
2017-03-14 17:01:30 +03:00
tbl_names_list.append([tbl_name, tbl_descr])
one_Table_descr.append(tbl_name)
2017-03-13 16:50:33 +03:00
x = 0
# список всех полей таблицы
qwery_create = "CREATE TABLE " + tbl_name + " ("
2017-03-14 17:01:30 +03:00
#qwery_select = "SELECT * FROM "
2017-03-13 16:50:33 +03:00
index = ""
2017-03-14 17:01:30 +03:00
field_names_list = []
2017-03-13 16:50:33 +03:00
while x < len(tbl_list[i]["fieldList"]):
if tbl_list[i]["fieldList"][x]["autoIncrement"] == "yes":
auto_increment = " AUTO_INCREMENT, "
else:
auto_increment = ", "
qwery_create = qwery_create + tbl_list[i]["fieldList"][x]["fName"] + " " + \
tbl_list[i]["fieldList"][x]["fType"] + auto_increment
if tbl_list[i]["fieldList"][x]["index"] == "PRIMARY KEY":
index = "PRIMARY KEY(" + tbl_list[i]["fieldList"][x]["fName"] + ")"
2017-03-14 17:01:30 +03:00
field_names_list.append(tbl_list[i]["fieldList"][x]["fDescription"])
2017-03-13 16:50:33 +03:00
x = x + 1
qwery_create = qwery_create + index + ");"
2017-03-14 17:01:30 +03:00
one_Table_descr.append(field_names_list)
#print(qwery_create)
2017-03-13 16:50:33 +03:00
i = i + 1
2017-03-14 17:01:30 +03:00
tbl_descr_list.append(one_Table_descr)
#print(tbl_descr_list)
2017-03-13 16:50:33 +03:00
return tbl_names_list
def initDBstructure():
2017-03-14 17:01:30 +03:00
global tbl_descr_list
2017-03-13 16:50:33 +03:00
table_list = open("tables.json", "r", encoding="utf-8")
data = json.load(table_list, encoding="utf-8")
tbl_list = data["tables"]
tbl_names_list = createTables(tbl_list)
2017-03-14 17:01:30 +03:00
2017-03-13 16:50:33 +03:00
return tbl_names_list
def selectData(table_name):
# получаем данные из таблицы
data_list = ("1","2","3","4","5")
return table_name
2017-03-14 17:01:30 +03:00
# получаем на вход имя таблицы и возвращаем список заголовков полей
def getTablesStructure(tbl):
global tbl_descr_list
for item in tbl_descr_list:
if item[0] == tbl:
return item[1]
#print(tbl_descr_list)
#return tbl_descr_list
#initDBstructure()