2013-03-14 1 views
6

私はmysqlデータベースと話すためにMySQLdbを使用しており、すべての結果セットを動的に取り出すことができます。mysqldbはタイムスタンプデータをNoneに変換します

私の問題は、結果セットを取得すると、mysqlにタイムスタンプとして宣言されている列がいくつかありますが、取得時にNoneになります。

私は2つの列を持っていますが、どちらもタイムスタンプとして宣言されていますが、一方は正しいデータを返します。 utimeとenddateの両方がタイムスタンプとして宣言されていますが、enddateの間はutimeは正しく返されません。誰も答えていないか、より多くの情報を見つけることを試みた後

['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 

答えて

7

は最後に、私は先に行って、より多くのソリューションを探したとのMySQLdbライブラリは、SQLからのpythonにデータ型を変換し、タイムスタンプを変換しませんバグがあることがわかりました。

私はまだそれらの1つが変換され、もう1つは変換されていない理由は分かりません。もし誰かがそれを理解できたら、これを更新してください。

しかし、ここでは、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 
2

をシリアル化することはできません私は同じ問題につまずいた:Noneを返す型DATETIME(1)のデータを取得します。

一部の研究ではMySQLdb-Bug #325が公開されました。そのバグトラッカーによれば、問題はまだ開いていますが(2年以上経過しています)、コメントは有効な解決策を提供します:

times.pyのMySQLdbパッケージでは、このようなマイクロ秒:

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 

私は、しかし、あなたの元のクエリが1列にしていないことで、他の...たぶん、第二は持っていない任意のマイクロ-情報に取り組んで説明することはできませんか?

+0

なぜこの回答が投票されたのですか?事実、3年後にもまだバグは修正されていませんでしたが、私にとっては興味深い事実でした(そして私は他の人にもそうなると信じています)。さらに、それは私のために働くソースと解決策を述べています。 – Fantilein1990

+0

私は分かりません。これは私にとって有望ですが、あなたの答えがなければ、私は回避策なしで残されます。 – thegiffman

関連する問題