Fixed "Add records" procedure (sql qwery now is correct). Added button on "Add record" form for colling relations table.

This commit is contained in:
Sergey Kalinin
2017-03-24 15:34:19 +03:00
parent 0ed5789c35
commit 2234722708
2 changed files with 163 additions and 146 deletions

96
dm.py
View File

@@ -67,7 +67,7 @@ def firstInit():
def dbConnect():
global c, db_type, db_hostname, db_user, db_password, db_name
global c, db_type, db_hostname, db_user, db_password, db_name, conn
if db_type == "mysql":
import pymysql
conn = pymysql.connect(
@@ -76,6 +76,7 @@ def dbConnect():
passwd=db_password,
host=db_hostname,
charset='utf8')
conn.commit()
elif db_type == "sqlite":
import sqlite3
conn = sqlite3.connect(db_name)
@@ -84,7 +85,6 @@ def dbConnect():
exit()
#print(conn)
c = conn.cursor()
return c
def createTables(tbl_list):
@@ -121,19 +121,21 @@ def createTables(tbl_list):
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"] + ")"
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"]), ])
[tbl_list[i]["fieldList"][x]["fName"], fieldTypeConvert(tbl_list[i]["fieldList"][x]["fType"]), auto_increment.strip(', ')])
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 + index + ");"
qwery_create = qwery_create.strip(', ') + index + ');'
#qwery_create = qwery_create + index + ');'
#print(qwery_create)
elif db_type == 'sqlite':
while x < len(tbl_list[i]["fieldList"]):
if tbl_list[i]["fieldList"][x]["autoIncrement"] == "yes":
@@ -150,7 +152,7 @@ def createTables(tbl_list):
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"]), ])
[tbl_list[i]["fieldList"][x]["fName"], fieldTypeConvert(tbl_list[i]["fieldList"][x]["fType"]), auto_increment.strip(', ')])
if tbl_list[i]["fieldList"][x]["relation"]:
relation_list = [tbl_list[i]["fieldList"][x]["fName"]]
relation_list.append(tbl_list[i]["fieldList"][x]["relation"])
@@ -165,7 +167,7 @@ def createTables(tbl_list):
i = i + 1
tbl_descr_list.append(one_Table_descr)
tbl_struct_list.append(one_Table_struct)
print(qwery_create)
#print(qwery_create)
c.execute(qwery_create)
return tbl_names_list
@@ -212,7 +214,7 @@ def selectData(tbl):
subqwery = "(SELECT (" + field_replace + ") FROM " + subqwery +" WHERE "+ table1 + "." + field1 +"="+ tbl +"."+ field +") AS " + field
qwery = qwery.replace(field, subqwery)
qwery = qwery.rstrip(',') + " FROM " + tbl + " LIMIT 10000"
print(qwery)
#print(qwery)
c.execute(qwery)
return c.fetchall()
@@ -273,13 +275,41 @@ def getFieldType(tbl, field):
#print(item[1])
#return item[1]
def getRelationsForField(tblSearch, fieldName):
global tbl_struct_list, c, db_type
#print("Ищем связи для:" +tblSearch +','+ fieldName)
#print(tbl_struct_list)
#searchField = tblSearch + '.' + fieldName
dataList = []
for item in tbl_struct_list:
#print(item)
tblName = item[0]
for field in item[1]:
field = field[0]
for rel in item[2]:
if tblName == tblSearch:
# выдергиваем из списка название и поле нужной нам таблицы
if rel[0] == fieldName:
#print('table - ' + tblName)
##print('field - ' + fieldName)
#print('relation - ' + str(rel))
l = rel[1][0].split('.')
relTable = l[0]
relField = l[1]
replaceFields = rel[1][1]
# print('reltable - ' + relTable)
#print('relfield - ' + relField)
#print('relrelation - ' + replaceFields)
return [relTable, relField, replaceFields]
return "q"
# добавление записи в БД на вход принимает список вида:
# [table [['field_1', 'значение'], ['field_2', 'значение'],....., ['field_n', 'значение']]]
def insertDataIntoBD(dataList):
global c
global c, conn
dbConnect()
tableName = dataList[0]
#print(dataList)
#datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M")
qwery = 'INSERT INTO ' + tableName + ' ('
@@ -288,26 +318,30 @@ def insertDataIntoBD(dataList):
for item in dataList[1]:
fType = getFieldType(tableName, item[0])
#print(fType)
qweryField = qweryField + item[0] + ','
if fType == 'integer':
qweryData = qweryData + '' + item[1] + ','
elif fType == 'datetime':
#преобразуем дату всяко разно
dt = datetime.strptime(item[1], "%d.%m.%Y %H:%M")
item[1] = str(dt)
qweryData = qweryData + '\'' + item[1] + '\','
elif fType == 'date':
d = item[1].split('.')
myDate = d[2] + '-' + d[1] + '-' + d[1]
item[1] = str(myDate)
qweryData = qweryData + '\'' + item[1] + '\','
else:
qweryData = qweryData + '\'' + item[1] + '\','
# проверяем если значение поля пустое то в запрос оно не включается
if item[1] != '':
qweryField = qweryField + item[0] + ','
if fType == 'integer':
qweryData = qweryData + '' + item[1] + ','
elif fType == 'datetime':
# преобразуем дату всяко разно
dt = datetime.strptime(item[1], "%d.%m.%Y %H:%M")
item[1] = str(dt)
qweryData = qweryData + '\'' + item[1] + '\','
elif fType == 'date':
d = item[1].split('.')
myDate = d[2] + '-' + d[1] + '-' + d[1]
item[1] = str(myDate)
qweryData = qweryData + '\'' + item[1] + '\','
else:
qweryData = qweryData + '\'' + item[1] + '\','
qwery = qwery + qweryField.rstrip(',') + ')' + ' VALUES (' + qweryData.rstrip(',') + ');'
#print(qwery)
print(qwery)
c.execute(qwery)
conn.commit()
#c.close()
return
@@ -342,16 +376,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":
print(db_type)
print(fieldReplace)
#print(db_type)
#print(fieldReplace)
fieldReplace = fieldReplace.replace(",", " || ' ' ||")
print(fieldReplace)
#print(fieldReplace)
subqwery = "(SELECT (" + fieldReplace + ") FROM " + subqwery +" WHERE "+ table1 + "." + field1 +"="+ tblName +"."+ field +") AS " + field
print("---" + subqwery)
#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()