python - 执行 python 脚本时,列 'date' 的值超出范围

标签 python mysql csv dictionary

我创建了一个 python 脚本,它读取 csv 文件,然后使用字典将数据存储到变量中。 然后我将变量插入 mysql 数据库。 每次都 100% 工作..除非我尝试插入日期。

我收到错误:

Out of range value for column 'date'

我打印了可变日期:2015-02-28 这正是我所需要的!但我仍然收到错误消息.. 此外,它将值 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

最佳答案

我终于找到了解决我的问题的方法! :) 我无法将 python 中的变量日期插入 mysql 表..出于一个奇怪的原因,它们不兼容.. 所以我创建了一个 sql 代码,它将获取昨天的日期并插入它,而无需使用 python 日期变量:) 我只是使用:

DATE_ADD(CURDATE(), INTERVAL -1 day)

这是我的代码 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()

关于python - 执行 python 脚本时,列 'date' 的值超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28632601/

相关文章:

python - distplot() 得到了一个意外的关键字参数 'figsize'

python - 正斜杠的正则表达式含义/

python - 在Python中从数据列表中选择一个字符串

Mysql子字符串并将诸如...之类的字符附加到结果命令

python - 写入 csv 时使用字典键作为坐标

java - 将文件中的无序项放入数组中

python - 上传文件到jupyter时忽略ds_store文件的建议

mysql - 如何使用 python 访问位于不同位置(非本地)的另一台计算机上的 MySQL 数据库

mysql - 如何从 Node.js 中运行 Mysql Select 的函数返回值

regex - Vim 正则表达式 : overwritten back references?