Home > Article > Backend Development > python3+pywin32 Obtain user-defined ODBC data source_PHP tutorial
Under Windows, obtain user-defined data sources by reading the registry
[python]
# -*- coding: UTF-8 -*-
# Get ODBC data source list
from tkinter import *
from tkinter import ttk
import win32api,win32con
def GetODBCdsn():
Key = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER,
'SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources',0, win32con.KEY_ALL_ACCESS)
#print(key)
#print(win32api.RegQueryValue(key,''))
#print('Return the number of sub-items, the number of item values, and the last modification time', win32api.RegQueryInfoKey(key))
Subitem, item, opdate =win32api.RegQueryInfoKey(key)
dsnlist=[]
for i in range(item):
print('---',win32api.RegEnumValue(key, i))
dsnName,dsnObject,dsnType = win32api.RegEnumValue(key, i)
dsnlist.append(dsnName)
#print(dir(win32api))
Win32api.RegCloseKey(key)
Return dsnlist
class MFrame(Frame):
Def __init__(self, master=None, cnf={}, **kw):
self.master = master
self.master.title('Get user-defined data source')
self.combo = ttk.Combobox(self.master)
self.combo.config(state="readonly")
self.combo.pack(side=TOP, fill = 'x', expand = False)
self.combo.update_idletasks()
comlist=GetODBCdsn()
self.combo['values'] = comlist
def test():
GetODBCdsn()
def main():
Root = Tk()
mf=MFrame(root)
Root.mainloop()
if __name__=="__main__":
#test()
main()