Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MS SQL时间数据类型转换为python时间_Python_Sql Server_Time - Fatal编程技术网

MS SQL时间数据类型转换为python时间

MS SQL时间数据类型转换为python时间,python,sql-server,time,Python,Sql Server,Time,如何将MSSQL时间数据类型转换为python时间或字符串?MSSQL服务器中的列定义为时间(7) 在Windows7上,我使用Python3/adodbapi,列以字节形式返回: sql = 'SELECT top 1 * FROM table1' with adodbapi.connect('srv') as cn: with cn.cursor() as cur: cur.execute(sql) for row in cur:

如何将MSSQL时间数据类型转换为python时间或字符串?MSSQL服务器中的列定义为时间(7)

在Windows7上,我使用Python3/adodbapi,列以字节形式返回:

sql = 'SELECT top 1 * FROM table1'
with adodbapi.connect('srv') as cn:
    with cn.cursor() as cur:
        cur.execute(sql)
        for row in cur:
            print(type(row['col1']))
            print(row['col1'])
            for c in row['col1']: print(c)

>>> <class 'bytes'>
>>> b'\t\x00\x1e\x00\x00\x00\x00\x00\x00\x00\x00\x00'
>>> 9
    0
    30
    0
    0
    0
    0
    0
    0
    0
    0
    0 
sql='从表1中选择前1个*
使用adodbapi.connect('srv')作为cn:
使用cn.cursor()作为cur:
当前执行(sql)
对于cur中的行:
打印(类型(行['col1']))
打印(第['col1'行])
对于第['col1']行中的c:打印(c)
>>> 
>>>b'\t\x00\x1e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
>>> 9
0
30
0
0
0
0
0
0
0
0
0
如何将python字节转换回时间或字符串

在linux上,我使用的是Python3/pymssql,列以字符串形式返回,我可以将其解析为时间

>>> <class 'str'>
>>> 09:30:00.0000000
>
>>> 09:30:00.0000000

我的解决方法是调整sql语句,以便tsql明确地转换time()列:

sql = 'SELECT *, convert(smalldatetime, col1) as ncol1  FROM table1'
在windows(adodbapi)和linux(pymssql)上,我现在为“ncol1”获得了一致的python日期时间对象

更新:
这看起来像是db驱动程序问题。在Windows上切换到pymssql之后,我发现数据库中的时间(7)列也以字符串形式返回,与Linux上的相同。(因此,Linux/windows和pymssql都为时间列返回字符串,这是一致的。)出于某种原因,adodbapi由teArray返回。

也许这个答案可以帮助您:我最终使用convert(smalldatetime,col1)调整sql语句作为时间1,如果你已经设法自己解决了你的问题,请详细说明你做了什么,作为回答,以便将来的读者能够从中受益。你也可以接受自己的答案。