mkleehammer / pyodbc

Python ODBC bridge
https://github.com/mkleehammer/pyodbc/wiki
MIT No Attribution
2.92k stars 561 forks source link

Insert into temp table with fast_executemany + executemany loses fraction of seconds #1219

Closed zhiyongzhang87 closed 1 year ago

zhiyongzhang87 commented 1 year ago

Environment

To diagnose, we usually need to know the following, including version numbers. On Windows, be sure to specify 32-bit Python or 64-bit:

Issue

import pyodbc, datetime, struct

def datetime_as_string(raw_bytes): tup = struct.unpack("<2l", raw_bytes) days_since_1900 = tup[0] partial_day = round(tup[1] / 300.0, 3) return datetime.datetime(1900, 1, 1) + datetime.timedelta(days=days_since_1900) + datetime.timedelta(seconds=partial_day)

cnx = pyodbc.connect('....') cnx.add_output_converter(pyodbc.SQL_TYPE_TIMESTAMP, datetime_as_string) cur = cnx.cursor() cur.fast_executemany = True cur.execute("CREATE TABLE #tmptest (dt DATETIME)") cur.execute("INSERT INTO #tmptest VALUES('2023-05-17 15:00:55.23')") cur.executemany('INSERT INTO #tmptest VALUES(?)', [[datetime.datetime(2023,5,17,15,0,55,230000)]]) cur.fast_executemany = False cur.executemany('INSERT INTO #tmptest VALUES(?)', [[datetime.datetime(2023,5,17,15,0,55,230000)]]) print(cur.execute('SELECT * FROM #tmptest').fetchall())

Output: [(datetime.datetime(2023, 5, 17, 15, 0, 55, 230000),), (datetime.datetime(2023, 5, 17, 15, 0, 55),), (datetime.datetime(2023, 5, 17, 15, 0, 55, 230000),)]

fast_executemany + executemany loses fraction of seconds

v-chojas commented 1 year ago

Does it only happen with a temp table, and not persistent tables? If so, try enabling the UseFMTONLY option. Otherwise, post an ODBC trace so we can inspect it to see what's going on.

zhiyongzhang87 commented 1 year ago

@v-chojas UseFMTONLY fixes the issue! thank you!