Some changes

This commit is contained in:
Sergey Kalinin
2017-12-04 13:19:04 +03:00
parent 9904d5b4e9
commit 4efc72e071
3 changed files with 24 additions and 213 deletions

35
dm.py
View File

@@ -30,6 +30,7 @@ def firstInit():
# Если конфиг уже есть читаем его если нет, создаем и потом читаем
if os.path.isfile(cfg_file):
print('Read config -' + cfg_file)
config.read(cfg_file)
else:
# Запись конфигурации в файл 'example.cfg'
@@ -42,13 +43,14 @@ def firstInit():
template_dir = config.get('Directory', 'template_dir')
db_type = config.get('DataBase', 'db_type')
db_hostname = config.get('DataBase', 'db_hostname')
db_name = config.get('DataBase', 'db_name')
template_file = os.path.join(template_dir, "{}.json".format(db_name))
if db_type == 'sqlite':
db_name = os.path.join(work_dir, config.get('DataBase', 'db_name'))
else:
db_name = config.get('DataBase', 'db_name')
db_name = "{}.{}".format(os.path.join(work_dir, db_name), 'sqlite')
db_user = config.get('DataBase', 'db_user')
db_password = config.get('DataBase', 'db_password')
# Создаём нужные каталоги
if os.path.isdir(work_dir):
print(work_dir + " already exists")
@@ -59,15 +61,17 @@ def firstInit():
else:
os.mkdir(template_dir)
# копируем и читаем файл шаблон БД
template_file = os.path.join(template_dir, 'tables.json')
#print("template file "+ template_file)
#template_file = os.path.join(template_dir, 'tables.json')
if os.path.isfile(template_file):
print("Template file already exists")
else:
shutil.copy('tables.json', template_file)
print("Template file " + template_file +" already exists")
# else:
# shutil.copy(template_file_name, template_file)
def dbConnect():
global c, db_type, db_hostname, db_user, db_password, db_name, conn
#print(db_name)
if db_type == "mysql":
import pymysql
conn = pymysql.connect(
@@ -146,8 +150,15 @@ def createTables(tbl_list):
index = " PRIMARY KEY"
else:
index = ''
qwery_create = qwery_create + tbl_list[i]["fieldList"][x]["fName"] + ' ' + \
tbl_list[i]["fieldList"][x]["fType"] + index + auto_increment
fieldType = tbl_list[i]["fieldList"][x]["fType"].lower()
#print(fieldType)
if fieldType[0:3] == 'int':
qwery_create = qwery_create + tbl_list[i]["fieldList"][x]["fName"] + ' ' + \
'INTEGER' + index + auto_increment
#print(fieldType)
else:
qwery_create = qwery_create + tbl_list[i]["fieldList"][x]["fName"] + ' ' + \
tbl_list[i]["fieldList"][x]["fType"] + index + auto_increment
field_names_list.append([tbl_list[i]["fieldList"][x]["fName"], tbl_list[i]["fieldList"][x]["fDescription"]])
# struct_fields_list.append(tbl_list[i]["fieldList"][x]["fName"])
@@ -167,12 +178,13 @@ def createTables(tbl_list):
i = i + 1
dbTablesDescriptionList.append(one_Table_descr)
dbTablesStructList.append(one_Table_struct)
#print(qwery_create)
print(qwery_create)
c.execute(qwery_create)
return dbTablesNamesList
def initDBstructure():
global dbTablesDescriptionList, template_file, tblNamesList
#print(template_file)
table_list = open(template_file, "r", encoding="utf-8")
data = json.load(table_list, encoding="utf-8")
tbl_list = data["tables"]
@@ -502,4 +514,5 @@ def editDataIntoBD(dataList):
#initDBstructure()
firstInit()
print(template_file)
dbConnect()