从python代码创建ODBC 64位用户dsn

时间:2018-12-17 11:53:04

标签: python dynamic odbc datasource dsn

我已使用以下代码从python代码创建dsn。

import ctypes

ODBC_ADD_DSN = 1        # Add data source
ODBC_CONFIG_DSN = 2     # Configure (edit) data source
ODBC_REMOVE_DSN = 3     # Remove data source
ODBC_ADD_SYS_DSN = 4    # add a system DSN
ODBC_CONFIG_SYS_DSN = 5 # Configure a system DSN
ODBC_REMOVE_SYS_DSN = 6 # remove a system DSN

def create_sys_dsn(driver, **kw):
    """Create a  system DSN
    Parameters:
        driver - ODBC driver name
        kw - Driver attributes
    Returns:
        0 - DSN not created
        1 - DSN created
    """
    nul = chr(0)
    attributes = []
    for attr in kw.keys():
        attributes.append("%s=%s" % (attr, kw[attr]))

    return ctypes.windll.ODBCCP32.SQLConfigDataSource(0, ODBC_ADD_SYS_DSN, driver, nul.join(attributes))

if __name__ == "__main__":
    if create_sys_dsn("SQL Server",SERVER="(localhost)", DESCRIPTION="Northwind SQL Server DSN", DSN="NorthwindDSN", Database="Northwind", Trusted_Connection="Yes"):
        print ("DSN created")
    else:
        print ("DSN not created")

但是我面临以下错误:-

回溯(最近通话最近):   在第27行的文件“ testDB.py”中     如果create_sys_dsn(“ SQL Server”,SERVER =“(localhost)”,description =“ Northwind SQL Server DSN”,DSN =“ NorthwindDSN”,Database =“ Northwind”,Trusted_Connection =“ Yes”):   create_sys_dsn中的文件“ testDB.py”,第24行     返回ctypes.windll.ODBCCP32.SQLConfigDataSource(0,ODBC_ADD_SYS_DSN,驱动程序,nul.join(属性)) ctypes.ArgumentError:参数4::嵌入空字符

谁能帮我解决这个问题。

0 个答案:

没有答案