Home >php教程 >php手册 >python3+pywin32 获取用户定义的ODBC数据源

python3+pywin32 获取用户定义的ODBC数据源

WBOY
WBOYOriginal
2016-06-13 10:52:50806browse

windows下,通过读取注册表获取用户定义的数据源

[python] 
# -*- coding: UTF-8 -*- 
# 获取ODBC数据源列表 
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('返回项的子项数目、项值数目,以及最后一次修改时间',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('获取用户定义的数据源') 
        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() 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn