推荐网址:https://www.jb51.net/article/100747.html
MySQL批量插入的语法为:
UPDATE nine_tupleSET SENTI_VALUE = CASE N_T_ID WHEN 1 THEN ‘1.57’ WHEN 2 THEN ‘1.3’ WHEN 3 THEN ‘1.2’ END
WHERE N_T_ID IN (1,2,3)
主要思想:将42W条数据分批次进行更新,我这里分42次,即每次批量更新1万条,然后编写Python程序按上面的语法格式构建相应的SQL语句,代码如下:
f=open("senti_values.txt",'r',encoding='utf8')res=f.readlines()for i in range(0,42):sql1 = "UPDATE nine_tuple SET SENTI_VALUE = CASE N_T_ID "sql2 = '1'for j in range(j*10000, (j+1)*10000):r = res[j].split('\n')[0].split(';')ntid = int(r[0])f_senti_sala = float(r[1])senti_sala = str(float('%.3f' % f_senti_sala))sql1 += ' WHEN %d THEN \'%s\'' % (ntid, senti_sala)if (i != 0):sql2 += ',' + str(ntid)sql = sql1 + ' END' + " WHERE N_T_ID IN (%s)" % (sql2)print(str(i + 1))cursor.execute(sql)db.commit()db.close()print('OK')