Code refactoring (use format() command for generate qwery string, rename some variables)

This commit is contained in:
svk28 2017-04-11 12:53:32 +03:00
parent a4528dccef
commit c6bf9f6d81

24
dm.py
View File

@ -315,7 +315,8 @@ def insertDataIntoBD(dataList):
#print(dataList)
#datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M")
qwery = 'INSERT INTO ' + tableName + ' ('
#qwery = 'INSERT INTO ' + tableName + ' ('
qwery = 'INSERT INTO {} ('.format(tableName)
qweryData = ''
qweryField = ''
for item in dataList[1]:
@ -323,24 +324,31 @@ def insertDataIntoBD(dataList):
#print(fType)
# проверяем если значение поля пустое то в запрос оно не включается
if item[1] != '':
qweryField = qweryField + item[0] + ','
#qweryField = qweryField + item[0] + ','
qweryField = '{}{},'.format(qweryField,item[0])
if fType == 'integer':
qweryData = qweryData + '' + item[1] + ','
#qweryData = qweryData + '' + item[1] + ','
qweryData = '{}{},'.format(qweryData,item[1])
elif fType == 'datetime':
# преобразуем дату всяко разно
dt = datetime.strptime(item[1], "%d.%m.%y %H:%M")
item[1] = str(dt)
qweryData = qweryData + '\'' + item[1] + '\','
#qweryData = qweryData + '\'' + item[1] + '\','
qweryData = "{}'{}',".format(qweryData,item[1])
elif fType == 'date':
d = item[1].split('.')
myDate = d[2] + '-' + d[1] + '-' + d[1]
#myDate = d[2] + '-' + d[1] + '-' + d[1]
myDate = '{}-{}-{}'.format(d[2], d[1], d[1])
item[1] = str(myDate)
qweryData = qweryData + '\'' + item[1] + '\','
#qweryData = qweryData + '\'' + item[1] + '\','
qweryData = "{}'{}',".format(qweryData, item[1])
else:
qweryData = qweryData + '\'' + item[1] + '\','
#qweryData = qweryData + '\'' + item[1] + '\','
qweryData = "{}'{}',".format(qweryData, item[1])
qwery = qwery + qweryField.rstrip(',') + ')' + ' VALUES (' + qweryData.rstrip(',') + ');'
#qwery = qwery + qweryField.rstrip(',') + ')' + ' VALUES (' + qweryData.rstrip(',') + ');'
qwery = '{}{}) VALUES ({});'.format(qwery, qweryField.rstrip(','), qweryData.rstrip(','))
print(qwery)
c.execute(qwery)