Datetime mysqldb将时间戳数据转换为无
我正在使用MySQLdb与mysql数据库通信,并且能够动态检索所有结果集 我的问题是,一旦我得到结果集,mysql中就有几个列被声明为时间戳,但是当它被检索时,它变成了无 我有两个列,都是声明的时间戳,但一个返回正确的数据,而另一个则不返回。utime和enddate都是声明的时间戳,但当enddate返回时,utime不会正确返回Datetime mysqldb将时间戳数据转换为无,datetime,timestamp,type-conversion,mysql-python,Datetime,Timestamp,Type Conversion,Mysql Python,我正在使用MySQLdb与mysql数据库通信,并且能够动态检索所有结果集 我的问题是,一旦我得到结果集,mysql中就有几个列被声明为时间戳,但是当它被检索时,它变成了无 我有两个列,都是声明的时间戳,但一个返回正确的数据,而另一个则不返回。utime和enddate都是声明的时间戳,但当enddate返回时,utime不会正确返回 ['utime', 'userstr', 'vstr_client', 'enddate'] ((None, '000102030ff43260gg080900
['utime', 'userstr', 'vstr_client', 'enddate']
((None, '000102030ff43260gg0809000000000004', '7.7.0', '1970-01-01 12:00:00.000000'))
def parse_data_and_description(cursor, data):
res = []
cols = [d[0] for d in cursor.description]
print cols
print data
for i in data:
res.append(OrderedDict(zip(cols, i)))
return res
def call_multi_rs(sp, args):
rs_id=0;
conn = connect()
cursor = conn.cursor()
try:
conn.autocommit(True)
cursor.execute ("CALL %s%s" % (sp, args))
while True:
rs_id+=1
data = cursor.fetchone( )
listout = parse_data_and_description(cursor, data)
print listout
if cursor.nextset( )==None:
# This means no more recordsets available
break
最后,在没有人回答或试图查找更多信息之后,我继续寻找更多的解决方案,发现MySQLdb库将数据类型从sql转换为python,并且存在一个不转换时间戳的bug 我仍然不知道为什么其中一个被改造,而另一个没有。如果有人能弄明白,请更新这个 但这里是连接到mysql数据库时需要进行的修改。 MySQLdb无法序列化python datetime对象
try:
import MySQLdb.converters
except ImportError:
_connarg('conv')
def connect(host='abc.dev.local', user='abc', passwd='def', db='myabc', port=3306):
try:
orig_conv = MySQLdb.converters.conversions
conv_iter = iter(orig_conv)
convert = dict(zip(conv_iter, [str,] * len(orig_conv.keys())))
print "Connecting host=%s user=%s db=%s port=%d" % (host, user, db, port)
conn = MySQLdb.connect(host, user, passwd, db, port, conv=convert)
except MySQLdb.Error, e:
print "Error connecting %d: %s" % (e.args[0], e.args[1])
return conn
我偶然发现了同样的问题:检索
DATETIME(1)
-type的数据返回None
提出了一些研究。根据该bug追踪者的说法,该问题仍然悬而未决(至今已超过2年),但评论提供了一个可行的解决方案:
在MySQLdb包的times.py中,需要插入一些行来处理微秒,如下所示:
def DateTime_or_None(s):
if ' ' in s:
sep = ' '
elif 'T' in s:
sep = 'T'
else:
return Date_or_None(s)
try:
d, t = s.split(sep, 1)
if '.' in t:
t, ms = t.split('.',1)
ms = ms.ljust(6, '0')
else:
ms = 0
return datetime(*[ int(x) for x in d.split('-')+t.split(':')+[ms] ])
except (SystemExit, KeyboardInterrupt):
raise
except:
return Date_or_None(s)
def TimeDelta_or_None(s):
try:
h, m, s = s.split(':')
if '.' in s:
s, ms = s.split('.')
ms = ms.ljust(6, '0')
else:
ms = 0
h, m, s, ms = int(h), int(m), int(s), int(ms)
td = timedelta(hours=abs(h), minutes=m, seconds=s,
microseconds=ms)
if h < 0:
return -td
else:
return td
except ValueError:
# unpacking or int/float conversion failed
return None
def Time_or_None(s):
try:
h, m, s = s.split(':')
if '.' in s:
s, ms = s.split('.')
ms = ms.ljust(6, '0')
else:
ms = 0
h, m, s, ms = int(h), int(m), int(s), int(ms)
return time(hour=h, minute=m, second=s, microsecond=ms)
except ValueError:
return None
def DateTime\u或\u None(s):
如果s中有“”:
sep=''
删除s中的“T”:
sep='T'
其他:
返回日期\u或\u无
尝试:
d、 t=s.分割(9月1日)
如果t中的“.”:
t、 ms=t.split('.',1)
ms=ms.ljust(6,'0')
其他:
ms=0
return datetime(*[int(x)表示x在d.split('-')+t.split(':')+[ms]]
除了(系统退出、键盘中断):
提升
除:
返回日期\u或\u无
def时间增量或无:
尝试:
h、 m,s=s.split(“:”)
如果s中的“.”
s、 ms=s.split('.'))
ms=ms.ljust(6,'0')
其他:
ms=0
h、 m,s,ms=int(h),int(m),int(s),int(ms)
td=时间增量(小时=绝对值(h),分钟=米,秒=秒,
微秒=毫秒)
如果h<0:
返回-td
其他:
返回td
除值错误外:
#解包或int/float转换失败
一无所获
定义时间或无:
尝试:
h、 m,s=s.split(“:”)
如果s中的“.”
s、 ms=s.split('.'))
ms=ms.ljust(6,'0')
其他:
ms=0
h、 m,s,ms=int(h),int(m),int(s),int(ms)
返回时间(小时=小时,分钟=米,秒=秒,微秒=毫秒)
除值错误外:
一无所获
但是,我无法解释的是,您最初的查询是在一列上进行的,而不是在另一列上进行的。。也许,第二个答案中没有任何微秒信息?为什么这个答案被否决了?事实上,三年后这个bug仍然没有被修复,这对我来说是一个有趣的事实(我相信对其他人来说也是如此)。此外,它还说明了对我有效的来源和解决方案……我不知道,伙计。这对我来说很有希望,如果没有你的回答,我将没有解决办法。你们是通过pip安装MySQLdb的吗?我的py2.7安装(从2014年开始)的代码与您的帖子相同。我的py3.6安装(从2017年开始)有不同的代码,但仍能处理微秒。