python 使用数据库
##插入数据
import MySQLdb
def InsertModelDatatoSQL():
db = MySQLdb.connect("", “”, “”, “”, 3306, charset=’’)
ModelVersion = ‘’
CDCVersion = ‘BL250’
totalModelData = GetModelResFromLog()
for key,value in totalModelData.items():
cursor = db.cursor()
insert_sql = "insert into DMS_Model_result(session_id, ModelRes, DetailRes, ModelVersion, CDCVersion, input_time) " \
'values ("%s", "%s", "%s", "%s", "%s","%s")' % \
(key,value["ModelRes"],value["DetailRes"],ModelVersion, CDCVersion, datetime.datetime.now())
print 'insert_sql =',insert_sql
cursor.execute(insert_sql)
db.commit()
db.close()
##查询数据
def DiffRestoSQL():
db = MySQLdb.connect("", “”, “”, “”, , charset=‘utf8’)
totalModelData = GetModelResFromLog()
for key, value in totalModelData.items():
cursor = db.cursor()
insert_sql = “insert into DMS_Diff_result(session_id, truth, ModelRes, input_time) "
'values (”%s", “%s”, “%s”, “%s”)’ %
(key, value[“GTRes”], value[“ModelRes”], datetime.datetime.now())
print ‘insert_sql =’, insert_sql
cursor.execute(insert_sql)
db.commit()
db.close()