@@ -145,7 +145,8 @@ def typeof(variate):
145
145
class Db (object ):
146
146
__conn = None
147
147
__map = []
148
- __bind = []
148
+ __bindWhere = []
149
+ __bindData = []
149
150
__name = ''
150
151
__column = '*'
151
152
__alias = ''
@@ -199,7 +200,7 @@ def where(self, where=None):
199
200
return self
200
201
if typeof (where ) == 'dict' :
201
202
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" ))
203
204
204
205
if typeof (where ) == 'list' :
205
206
for item in where :
@@ -330,17 +331,17 @@ def find(self):
330
331
column = self .__getField ()
331
332
sql = self .__comQuerySql ()
332
333
if self .__build :
333
- return sql , self .__bind
334
+ return sql , self .__bindWhere
334
335
if sql is None :
335
336
return None
336
337
result = None
337
338
try :
338
339
self .__connect ()
339
- self .cursor .execute (sql , self .__bind )
340
+ self .cursor .execute (sql , self .__bindWhere )
340
341
result = self .cursor .fetchone ()
341
342
self .__close ()
342
343
except Exception as e :
343
- print (sql , self .__bind )
344
+ print (sql , self .__bindWhere )
344
345
print (e )
345
346
exit (- 1 )
346
347
if result is None :
@@ -358,17 +359,17 @@ def select(self):
358
359
359
360
sql = self .__comQuerySql ()
360
361
if self .__build :
361
- return sql , self .__bind
362
+ return sql , self .__bindWhere
362
363
if sql is None :
363
364
return None
364
365
result = None
365
366
try :
366
367
self .__connect ()
367
- self .cursor .execute (sql , self .__bind )
368
+ self .cursor .execute (sql , self .__bindWhere )
368
369
result = self .cursor .fetchall ()
369
370
self .__close ()
370
371
except Exception as e :
371
- print (sql , self .__bind )
372
+ print (sql , self .__bindWhere )
372
373
print (e )
373
374
exit (- 1 )
374
375
if result is None :
@@ -421,16 +422,19 @@ def insert(self, data):
421
422
if column ['field' ] == key :
422
423
if i == 0 :
423
424
fields = key
424
- values = format_field (data [key ], column ['type' ])
425
+ # values = format_field(data[key], column['type'])
426
+ values = '%s'
425
427
else :
426
428
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 ])
428
432
i += 1
429
433
if fields == '' or values == '' :
430
434
return 0
431
435
sql = str (" insert into " + self .__name + " ( " + fields + ") values ( " + values + " ) " )
432
436
433
- return self .__edit (sql )
437
+ return self .__add (sql )
434
438
435
439
def update (self , data ):
436
440
if typeof (data ) != 'dict' :
@@ -451,16 +455,20 @@ def update(self, data):
451
455
# values = format_field(item.get('val'), column['type'])
452
456
values = item .get ('val' )
453
457
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 ) '
455
460
else :
456
461
print ('禁止不使用 where 更新数据' )
457
462
for key in data :
458
463
for column in all_column :
459
464
if column ['field' ] == key :
460
465
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'
462
468
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 ])
464
472
i += 1
465
473
if fields == '' :
466
474
return 0
@@ -558,11 +566,26 @@ def __getPk(self):
558
566
559
567
def __edit (self , sql ):
560
568
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
562
585
count = 0
563
586
try :
564
587
self .__connect ()
565
- count = self .cursor .execute (sql )
588
+ count = self .cursor .execute (sql , self . __bindData )
566
589
self .__conn .commit ()
567
590
self .__close ()
568
591
except Exception as e :
@@ -573,7 +596,8 @@ def __edit(self, sql):
573
596
574
597
def clear (self ):
575
598
self .__map = []
576
- self .__bind = []
599
+ self .__bindWhere = []
600
+ self .__bindData = []
577
601
self .__name = ''
578
602
self .__column = '*'
579
603
self .__alias = ''
0 commit comments