简体   繁体   中英

Getting lookup value from MS Access with pyodbc

I have a access Database, where some of the cells have been set as lookup to other tabels and this works as it should.

But when I query it from my Python code, I only recieve the number it is placed in the tabel. How do I get the text?

conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, accdb)};DBQ=' + filepath + ';')
cursor = conn.cursor()

cursor.execute("Select Tagname, PV, OUT from PID")

for row in cursor:
            Tagname.append(row[0])
            arrIn.append(row[1])
            arrOut.append(row[2])

return Tagname, arrIn, arrOut

I'm using Python 3.7 and pyodbc

regards Mads

You need to modify the query to make a JOIN of the relevant tables. Since you don't describe your database or which columns in the PID table that represents foreign keys, it is hard to give a complete answer. It should look something like this:

cursor.execute("SELECT PID.Tagname, PID.PV, PID.OUT FROM PID INNER JOIN some_other_table ON (PID.Tagname=some_other_table.some_id)")

You can also lookup JOIN queries elsewhere and find a better example representing your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM