Good Morning.
I'm new to Python and I'm doing a internship at the moment. One part of the script that they want me to make is to import a table from Access database 1 to Access database 2.
I was trying to do something with the 2 following libaries: pyodbc and prettytable. Where I wanted to make a temporary table with prettytable from database 1 and get the values from that and put it in database 2. Where I was hoping that I can put a variable in the SQL. But obvious, that never work. So I'm stuck right now.
Has somebody an idea?
You can read my beautiful code that I use below here:
import pyodbc
DBfile = 'C:/Users/stage1/Documents/test.accdb'
conn = pyodbc.connect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:/Users/stage1/Documents/test.accdb;")
cursor = conn.cursor()
DBfile = 'C:/Users/stage1/Documents/test.accdb'
conn2 = pyodbc.connect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:/Users/stage1/Documents/test.accdb;")
cursor2 = conn2.cursor()
SQL = """select naam
FROM naam;"""
for row in cursor.execute(SQL):
k = row.naam
print k
cursor2.execute("""insert into testtable(naam) values (?)""", (k))
conn2.commit()
cursor.close()
conn.close()
cursor2.close()
conn2.close()
THE PROBLEM IS SOLVED. Thanks to mhawke
cursorobject inside the loop. If you are iterating over the rows in a cursor object namedcursoryou'll need to use a different cursor object (e.g., one namedcursor2) to perform the insert.