mysql中字典值怎么添加_插入Python字典中的值,包括MySQL的键

我有以下字典:{ '': ['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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值