Fixed sqlite "create table" procedure.

This commit is contained in:
Sergey Kalinin 2017-03-23 16:35:44 +03:00
parent bf9dd44d62
commit 0ed5789c35

79
dm.py
View File

@ -88,7 +88,7 @@ def dbConnect():
return c
def createTables(tbl_list):
global tbl_descr_list, tbl_struct_list, c
global tbl_descr_list, tbl_struct_list, c, db_type
i = 0
tbl_names_list = []
tbl_descr_list = []
@ -111,26 +111,54 @@ def createTables(tbl_list):
index = ""
field_names_list = []
struct_fields_list = []
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"] + ")"
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"])
struct_fields_list.append([tbl_list[i]["fieldList"][x]["fName"], fieldTypeConvert(tbl_list[i]["fieldList"][x]["fType"]), ])
if tbl_list[i]["fieldList"][x]["relation"]:
relation_list=[tbl_list[i]["fieldList"][x]["fName"]]
relation_list.append(tbl_list[i]["fieldList"][x]["relation"])
# формируем запрос в зависимости от типа БД
if db_type == 'mysql':
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"] + ")"
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"])
struct_fields_list.append(
[tbl_list[i]["fieldList"][x]["fName"], fieldTypeConvert(tbl_list[i]["fieldList"][x]["fType"]), ])
if tbl_list[i]["fieldList"][x]["relation"]:
relation_list = [tbl_list[i]["fieldList"][x]["fName"]]
relation_list.append(tbl_list[i]["fieldList"][x]["relation"])
one_Table_relation.append(relation_list)
x = x + 1
one_Table_relation.append(relation_list)
x = x + 1
qwery_create = qwery_create + index + ");"
elif db_type == 'sqlite':
while x < len(tbl_list[i]["fieldList"]):
if tbl_list[i]["fieldList"][x]["autoIncrement"] == "yes":
auto_increment = " AUTOINCREMENT,"
else:
auto_increment = ", "
if tbl_list[i]["fieldList"][x]["index"] == "PRIMARY KEY":
index = " PRIMARY KEY"
else:
index = ''
qwery_create = qwery_create + tbl_list[i]["fieldList"][x]["fName"] + ' ' + \
tbl_list[i]["fieldList"][x]["fType"] + index + auto_increment
qwery_create = qwery_create + index + ");"
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"])
struct_fields_list.append(
[tbl_list[i]["fieldList"][x]["fName"], fieldTypeConvert(tbl_list[i]["fieldList"][x]["fType"]), ])
if tbl_list[i]["fieldList"][x]["relation"]:
relation_list = [tbl_list[i]["fieldList"][x]["fName"]]
relation_list.append(tbl_list[i]["fieldList"][x]["relation"])
one_Table_relation.append(relation_list)
x = x + 1
qwery_create = qwery_create.strip(', ') + ");"
one_Table_struct.append(struct_fields_list)
one_Table_struct.append(one_Table_relation)
one_Table_descr.append(field_names_list)
@ -138,7 +166,7 @@ def createTables(tbl_list):
tbl_descr_list.append(one_Table_descr)
tbl_struct_list.append(one_Table_struct)
print(qwery_create)
#c.execute(qwery_create)
c.execute(qwery_create)
return tbl_names_list
def initDBstructure():
@ -160,7 +188,7 @@ def selectData(tbl):
if item[0] == tbl:
for field in item[1]:
field = field[0]
qwery = qwery + '\'' + field + '\'' + ","
qwery = qwery + field + ","
for rel in item[2]:
field = rel[0]
field_rel = rel[1][0]
@ -289,6 +317,7 @@ def selectDataFromDB(tblName, fieldName, fieldValue):
global tbl_struct_list, c, db_type
qwery = "SELECT "
subqwery = ""
for item in tbl_struct_list:
if item[0] == tblName:
for field in item[1]:
@ -313,12 +342,16 @@ def selectDataFromDB(tblName, fieldName, fieldValue):
fieldReplace = fieldReplace.replace(",", ",' ',")
subqwery = "(SELECT CONCAT(" + fieldReplace + ") FROM " + subqwery + " WHERE " + table1 + "." + field1 + "=" + tblName + "." + field + ") AS " + field
elif db_type == "sqlite":
field_replace = fieldReplace.replace(",", " || ' ' ||")
print(db_type)
print(fieldReplace)
fieldReplace = fieldReplace.replace(",", " || ' ' ||")
print(fieldReplace)
subqwery = "(SELECT (" + fieldReplace + ") FROM " + subqwery +" WHERE "+ table1 + "." + field1 +"="+ tblName +"."+ field +") AS " + field
print("---" + subqwery)
qwery = qwery.replace(field, subqwery)
#qwery = qwery.rstrip(',') + " FROM " + tblName + " LIMIT 10000"
qwery = qwery.rstrip(',') + " FROM " + tblName + " WHERE " + fieldName + '=' + fieldValue
#print(qwery)
print(qwery)
c.execute(qwery)
return c.fetchall()