日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

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

發(fā)布時(shí)間:2023/12/1 数据库 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql中字典值怎么添加_插入Python字典中的值,包括MySQL的键 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

我有以下字典:{ '': ['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數(shù)據(jù)庫(kù),例如:

^{pr2}$

我創(chuàng)建了一個(gè)python代碼來(lái)執(zhí)行這個(gè)操作,但是它沒(méi)有將值插入到數(shù)據(jù)庫(kù)中

我得到了一個(gè)錯(cuò)誤:mysql_異常操作錯(cuò)誤:(1136,“列計(jì)數(shù)與第1行的值計(jì)數(shù)不匹配”)

下面是我的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()

感謝下面的答案,我修復(fù)了我的問(wèn)題最終代碼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()

總結(jié)

以上是生活随笔為你收集整理的mysql中字典值怎么添加_插入Python字典中的值,包括MySQL的键的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。