Skip to content

Commit 7029257

Browse files
committed
TuuzPC
1 parent 4b3acfa commit 7029257

File tree

1 file changed

+41
-17
lines changed

1 file changed

+41
-17
lines changed

Database.py

+41-17
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ def typeof(variate):
145145
class Db(object):
146146
__conn = None
147147
__map = []
148-
__bind = []
148+
__bindWhere = []
149+
__bindData = []
149150
__name = ''
150151
__column = '*'
151152
__alias = ''
@@ -199,7 +200,7 @@ def where(self, where=None):
199200
return self
200201
if typeof(where) == 'dict':
201202
self.__map.append({'key': where.get('key'), 'val': "%s", 'type': where.get('type')})
202-
self.__bind.append(where.get("val"))
203+
self.__bindWhere.append(where.get("val"))
203204

204205
if typeof(where) == 'list':
205206
for item in where:
@@ -330,17 +331,17 @@ def find(self):
330331
column = self.__getField()
331332
sql = self.__comQuerySql()
332333
if self.__build:
333-
return sql, self.__bind
334+
return sql, self.__bindWhere
334335
if sql is None:
335336
return None
336337
result = None
337338
try:
338339
self.__connect()
339-
self.cursor.execute(sql, self.__bind)
340+
self.cursor.execute(sql, self.__bindWhere)
340341
result = self.cursor.fetchone()
341342
self.__close()
342343
except Exception as e:
343-
print(sql, self.__bind)
344+
print(sql, self.__bindWhere)
344345
print(e)
345346
exit(-1)
346347
if result is None:
@@ -358,17 +359,17 @@ def select(self):
358359

359360
sql = self.__comQuerySql()
360361
if self.__build:
361-
return sql, self.__bind
362+
return sql, self.__bindWhere
362363
if sql is None:
363364
return None
364365
result = None
365366
try:
366367
self.__connect()
367-
self.cursor.execute(sql, self.__bind)
368+
self.cursor.execute(sql, self.__bindWhere)
368369
result = self.cursor.fetchall()
369370
self.__close()
370371
except Exception as e:
371-
print(sql, self.__bind)
372+
print(sql, self.__bindWhere)
372373
print(e)
373374
exit(-1)
374375
if result is None:
@@ -421,16 +422,19 @@ def insert(self, data):
421422
if column['field'] == key:
422423
if i == 0:
423424
fields = key
424-
values = format_field(data[key], column['type'])
425+
# values = format_field(data[key], column['type'])
426+
values = '%s'
425427
else:
426428
fields += ',' + key
427-
values += ',' + format_field(data[key], column['type'])
429+
# values += ',' + format_field(data[key], column['type'])
430+
values += ', %s '
431+
self.__bindData.append(data[key])
428432
i += 1
429433
if fields == '' or values == '':
430434
return 0
431435
sql = str(" insert into " + self.__name + " ( " + fields + ") values ( " + values + " ) ")
432436

433-
return self.__edit(sql)
437+
return self.__add(sql)
434438

435439
def update(self, data):
436440
if typeof(data) != 'dict':
@@ -451,16 +455,20 @@ def update(self, data):
451455
# values = format_field(item.get('val'), column['type'])
452456
values = item.get('val')
453457
break
454-
sql += ' and ( ' + item.get('key') + ' ' + item.get('type') + ' ' + values + ' ) '
458+
# sql += ' and ( ' + item.get('key') + ' ' + item.get('type') + ' ' + values + ' ) '
459+
sql += ' and ( ' + item.get('key') + ' ' + item.get('type') + ' %s ) '
455460
else:
456461
print('禁止不使用 where 更新数据')
457462
for key in data:
458463
for column in all_column:
459464
if column['field'] == key:
460465
if i == 0:
461-
fields = key + '=' + format_field(data[key], column['type'])
466+
# fields = key + '=' + format_field(data[key], column['type'])
467+
fields = key + '=%s'
462468
else:
463-
fields += str(',' + key + '=' + format_field(data[key], column['type']))
469+
# fields += str(',' + key + '=' + format_field(data[key], column['type']))
470+
fields += str(',' + key + '=%s')
471+
self.__bindData.append(data[key])
464472
i += 1
465473
if fields == '':
466474
return 0
@@ -558,11 +566,26 @@ def __getPk(self):
558566

559567
def __edit(self, sql):
560568
if self.__build:
561-
return sql
569+
return sql, self.__bindData + self.__bindWhere
570+
count = 0
571+
try:
572+
self.__connect()
573+
count = self.cursor.execute(sql, self.__bindData + self.__bindWhere)
574+
self.__conn.commit()
575+
self.__close()
576+
except Exception as e:
577+
self.__conn.rollback()
578+
print('Error: ', sql)
579+
print(e)
580+
return count
581+
582+
def __add(self, sql):
583+
if self.__build:
584+
return sql, self.__bindData
562585
count = 0
563586
try:
564587
self.__connect()
565-
count = self.cursor.execute(sql)
588+
count = self.cursor.execute(sql, self.__bindData)
566589
self.__conn.commit()
567590
self.__close()
568591
except Exception as e:
@@ -573,7 +596,8 @@ def __edit(self, sql):
573596

574597
def clear(self):
575598
self.__map = []
576-
self.__bind = []
599+
self.__bindWhere = []
600+
self.__bindData = []
577601
self.__name = ''
578602
self.__column = '*'
579603
self.__alias = ''

0 commit comments

Comments
 (0)