0
DU = []
for i in range(len(DD)): 
    DU += [DD[i]+' '+UU[i]]


dtDate = []
for i in range(len(DU)):
    dtDate = dtDate + [time.strptime(DU[i],"%d.%m.%Y %H:%M:%S")]

#readin access
path = './'
acc_fname = 'test.mdb'
DRV = '{Microsoft Access Driver (*.mdb, *.accdb)}'
MDB = path + acc_fname
PWD = ''
con = pyodbc.connect('DRIVER={};DBQ={};PWD={}'.format(DRV,MDB,PWD))
cur = con.cursor()
for i in range(3):
    SQL = 'SELECT ID FROM Tab WHERE Tab.FileZeit = {0};'.format(dtDate[i])
table_2 = cur.execute(SQL).fetchall()
print(table_2)

then i got this error:

pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC-Treiber f\ufffdr Microsoft Access] Undefined function 'time.struct_time' in Ausdruck. (-3102) (SQLExecDirectW)")

DD & UU Are lists with date and time, then i convert them to datetime(dtDate). Filezeit is also a datetime type.

RevanthKrishnaKumar V.
  • 1,855
  • 1
  • 21
  • 34
Nekoso
  • 113
  • 5

2 Answers2

0

Well, your db cant handle the time.struct_time, try to use a change it back to a string with SQL = 'SELECT ID FROM Tab WHERE Tab.FileZeit = {0};'.format(time.strftime(dtDate[i],"%d.%m.%Y %H:%M:%S"))

EDIT:

SQL = 'SELECT ID FROM Tab WHERE Tab.FileZeit = %s' % time.strftime("%d.%m.%Y %H:%M:%S", dtDate[i]))

axel_ande
  • 359
  • 1
  • 4
  • 20
0

Use parameters instead of string formatting for SQL queries. Add a parameter placeholder (?) to the query, and supply a value for that parameter when calling Cursor.execute.

con = pyodbc.connect('DRIVER={};DBQ={};PWD={}'.format(DRV,MDB,PWD))
cur = con.cursor()
for i in range(3):
    params = (dtDate[i],)
    SQL = 'SELECT ID FROM Tab WHERE Tab.FileZeit = ?;'
table_2 = cur.execute(SQL, params).fetchall()

When working with datetime values, this has the added benefit of letting pyodbc handle translation of Python datetime types to/from ODBC types, no string formatting required.

Bryan
  • 17,112
  • 7
  • 57
  • 80