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.

0

2 Answers 2

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]))

Sign up to request clarification or add additional context in comments.

3 Comments

then i got this error: TypeError: must be str, not time.struct_time
i used your edit version but the typeerror: must be str, not time.struct_time remains
Lol, it should be time.strftime("%d.%m.%Y %H:%M:%S", dtDate[i])
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.