I'm trying to insert values into a Microsoft Access Database using Python.
I'm able to insert values as follows:
df = pd.read_excel(xlsx, sheets[0])
for i in range(1, len(sheets)):
data = []
data = pd.read_excel(xlsx, sheets[i])
df = df.append(data)
k = (df.iat[3,0])
conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ= \\.....\Stat_tracker.accdb;')
cursor = conn.cursor()
cursor.execute("INSERT INTO ABCD (Serial) VALUES ('xxxx')")
cursor.execute('select * from ABCD')
for row in cursor.fetchall():
print(row)
I want to use a loop to iterate over multiple records. Is there a better way?
When I replace the cursor.execute code with
cursor.execute("INSERT INTO ABCD (Serial) VALUES (%s)",(k,))
It throws back the following error:
pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Microsoft Access Driver]
Syntax error in query expression '%s'. (-3100) (SQLPrepare)")
Clearly, I'm missing something with the use of %s. Perhaps there's another placeholder?
It should be a simple fix, I'm just too unfamiliar with the syntax.
Help a newbie out?
pyodbc, it should be qmark,?.