我创建了一个python脚本,它读取csv文件,然后使用字典将数据存储到变量中。 然后我将变量插入mysql数据库。 每次都是100%工作..除非我尝试插入日期。
我收到错误:
列'date'超出范围值
我打印了变量日期:2015-02-28 这正是我需要的!但我仍然收到错误消息.. 此外,它将value 0000-00-00而不是2015-02-28插入到我的表:s 我认为问题是2015-02-28可能是一个字符串..我怎么能把它转换成日期?
这是我的python脚本:
#4 python script to insert all data to mysql
#!/usr/bin/python
from StringIO import StringIO
import numpy as np
import csv
import MySQLdb
import os
from datetime import datetime,date,timedelta
dict= {}
infile= open('csv_err1.log','r')
lines= infile.readlines()
for i in lines:
eventKey, count, totalDuration, average = [a.strip() for a in i.split(',')]
dict.setdefault(eventKey, []).append((int(count), int(totalDuration), float(average)))
date = date.today() - timedelta(1)
app_launch_time =dict["app_launch_time"][0][0]
bup_login_error =dict["bup_login_error"][0][0]
crash =dict["crash"][0][0]
parental_controls_error =dict["parental_controls_error"][0][0]
playback_error =dict["playback_error"][0][0]
qp_library_failed_reauthentication =dict["qp_library_failed_reauthentication"][0][0]
qp_library_failed_to_start =dict["qp_library_failed_to_start"][0][0]
search_error =dict["search_error"][0][0]
video_load_time =dict["video_load_time"][0][0]
tbr_error =dict["tbr_error"][0][0]
live_channels_metadata_request_failed =dict["live_channels_metadata_request_failed"][0][0]
vod_catalog_metadata_request_failed =dict["vod_catalog_metadata_request_failed"][0][0]
app_launch_time_avg =dict["app_launch_time"][0][2]
video_load_time_avg =dict["video_load_time"][0][2]
print date
# 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 errorscounted (date,app_launch_time,bup_login_error,crash, parental_controls_error,playback_error,qp_library_failed_reauthentication,qp_library_failed_to_start,search_error,video_load_time,tbr_error,live_channels_metadata_request_failed,vod_catalog_metadata_request_failed,app_launch_time_avg,video_load_time_avg) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""" %(date,app_launch_time, bup_login_error, crash, parental_controls_error,playback_error,qp_library_failed_reauthentication,qp_library_failed_to_start,search_error,video_load_time,tbr_error,live_channels_metadata_request_failed,vod_catalog_metadata_request_failed,app_launch_time_avg,video_load_time_avg))
try:
# Execute the SQL command
cursor.execute(sql)
# Commit your changes in the database
db.commit()
for row in cursor.fetchall():
print row[0]
except:
# Rollback in case there is any error
db.rollback()
# disconnect from server
cursor.close()
db.close()
任何帮助或指示将不胜感激:)
编辑: 这是我的csv_err1.log文件:
app_launch_time,12247,118616277,9685.33
video_load_time,12966,123702815,9540.55
eventKey,2,0,0
playback_error,3773,0,0
qp_library_failed_reauthentication,230,0,0
search_error,183,0,0
epg_metadata_request_failed,5,0,0
live_channels_metadata_request_failed,13,0,0
vod_catalog_metadata_request_failed,1,0,0
bup_login_error,20,0,0
qp_library_failed_to_start,295,0,0
0,9,0,0
tbr_error,389,0,0
crash,218,0,0
parental_controls_error,123,0,0
答案 0 :(得分:1)
我终于找到了解决问题的方法! :) 我无法在python中将变量日期插入到mysql表中......出于一个奇怪的原因,它们不兼容.. 所以我创建了一个sql代码,可以获得昨天的日期并插入它而不必使用python日期变量:) 我只是用过:
DATE_ADD(CURDATE(),INTERVAL -1天)
这是我的代码100%防弹:
#!/usr/bin/python
from StringIO import StringIO
import numpy as np
import csv
import MySQLdb
import os
dict= {}
infile= open('csv_err1.log','r')
lines= infile.readlines()
for i in lines:
eventKey, count, totalDuration, average = [a.strip() for a in i.split(',')]
dict.setdefault(eventKey, []).append((int(count), int(totalDuration), float(average)))
app_launch_time =dict["app_launch_time"][0][0]
bup_login_error =dict["bup_login_error"][0][0]
crash =dict["crash"][0][0]
parental_controls_error =dict["parental_controls_error"][0][0]
playback_error =dict["playback_error"][0][0]
qp_library_failed_reauthentication =dict["qp_library_failed_reauthentication"][0][0]
qp_library_failed_to_start =dict["qp_library_failed_to_start"][0][0]
search_error =dict["search_error"][0][0]
video_load_time =dict["video_load_time"][0][0]
tbr_error =dict["tbr_error"][0][0]
live_channels_metadata_request_failed =dict["live_channels_metadata_request_failed"][0][0]
vod_catalog_metadata_request_failed =dict["vod_catalog_metadata_request_failed"][0][0]
app_launch_time_avg =dict["app_launch_time"][0][2]
video_load_time_avg =dict["video_load_time"][0][2]
print ("app_launch_time", app_launch_time)
print ("bup_login_error",bup_login_error)
print ("crash ", crash )
print ("parental_controls_error", parental_controls_error)
print ("playback_error", playback_error)
print ("qp_library_failed_reauthentication", qp_library_failed_reauthentication)
print ("qp_library_failed_to_start", qp_library_failed_to_start)
print ("search_error", search_error)
print ("Video_load", video_load_time)
print ("tbr_error", tbr_error)
print ("live_channels_metadata_request_failed", live_channels_metadata_request_failed)
print ("vod_catalog_metadata_request_failed", vod_catalog_metadata_request_failed)
print ("app_launch_time_avg", app_launch_time_avg)
print ("Video_load", video_load_time_avg)
# 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 errorscounted (date,app_launch_time,bup_login_error,crash, parental_controls_error,playback_error,qp_library_failed_reauthentication,qp_library_failed_to_start,search_error,video_load_time,tbr_error,live_channels_metadata_request_failed,vod_catalog_metadata_request_failed,app_launch_time_avg,video_load_time_avg) VALUES(DATE_ADD(CURDATE(), INTERVAL -1 day),%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""" %(app_launch_time,bup_login_error,crash,parental_controls_error,playback_error,qp_library_failed_reauthentication,qp_library_failed_to_start,search_error,video_load_time,tbr_error,live_channels_metadata_request_failed,vod_catalog_metadata_request_failed,app_launch_time_avg,video_load_time_avg))
try:
# Execute the SQL command
cursor.execute(sql)
# Commit your changes in the database
db.commit()
for row in cursor.fetchall():
print row[0]
except:
# Rollback in case there is any error
db.rollback()
# disconnect from server
cursor.close()
db.close()