我有以下字典:{ '': ['0', '9'],
'3904': ['playback_error', '87'],
'3808': ['playback_error', '24'],
'3902': ['qp_library_failed_to_start', '1'],
'3903': ['playback_error', '464'],
'3805': ['playback_error', '141'],
'3807': ['playback_error', '29'],
'3806': ['playback_error', '1'],
'1309': ['playback_error', '2'],
'3803': ['playback_error', '28'],
'BL-1008': ['parental_controls_error', '5'],
'errorCode': ['eventKey', '2'],
'404': ['tbr_error', '68'],
'3308': ['playback_error', '10']}
我想将这些值插入mysql数据库,例如:
^{pr2}$
我创建了一个python代码来执行这个操作,但是它没有将值插入到数据库中
我得到了一个错误:mysql_异常操作错误:(1136,“列计数与第1行的值计数不匹配”)
下面是我的python代码:data=mydict
print data
#print data.values()
# Open database connection
db = MySQLdb.connect(host,user,passwd,db)
cursor = db.cursor()
# Prepare SQL query to INSERT a record into the database.
sql = '''INSERT INTO errorscode (id,date,errorcode,eventkey,count) VALUES(NULL,(DATE_ADD(CURDATE(), INTERVAL -1 day),%s,%s,%s))'''
# Execute the SQL command
cursor.executemany(sql,data.values())
# Commit your changes in the database
db.commit()
# disconnect from server
cursor.close()
db.close()
感谢下面的答案,我修复了我的问题最终代码100%正常工作:):#!/usr/bin/python
from StringIO import StringIO
import numpy as np
import csv
import MySQLdb
import os
with open('csv_err2.log', mode='r') as infile:
reader = csv.reader(infile)
mydict = dict((rows[0],[rows[2], rows[1]]) for rows in reader)
data=mydict
print data
# Open database connection
db = MySQLdb.connect(host="localhost",user="root",passwd="bravoecholimalima",db="capacityreports_mobiletv")
cursor = db.cursor()
# Prepare SQL query to INSERT a record into the database.
sql = '''INSERT INTO errorscode (id,date,errorcode,count,eventkey) VALUES(NULL,(DATE_ADD(CURDATE(), INTERVAL -1 day)),%s,$
sql_values_list = list()
for key, value in data.iteritems():
sql_values_list.append((key,int(value[0]),value[1]))
print sql_values_list
# Execute the SQL command
cursor.executemany(sql, sql_values_list)
# Commit your changes in the database
db.commit()
# disconnect from server
cursor.close()
db.close()