1

I have used the following code to create dsn from python code.

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")

But i am facing the following error:-

Traceback (most recent call last): File "testDB.py", line 27, in if create_sys_dsn("SQL Server",SERVER="(localhost)", DESCRIPTION="Northwind SQL Server DSN", DSN="NorthwindDSN", Database="Northwind", Trusted_Connection="Yes"): File "testDB.py", line 24, in create_sys_dsn return ctypes.windll.ODBCCP32.SQLConfigDataSource(0, ODBC_ADD_SYS_DSN, driver, nul.join(attributes)) ctypes.ArgumentError: argument 4: : embedded null character

Can anyone please help me to resolve this.

learner
  • 332
  • 2
  • 6
  • 22
  • I'm somewhat suspecting the `nul = chr(0)`. Printing `chr(0)` under Windows looks like a white space char (for whatever reason); but in fact it is the NUL character. Try with `nul = chr(32)` or `nul = ' '` instead. – shmee Dec 17 '18 at 13:07
  • 1
    Hi thanks, that problem is resolved.. But currently using the above code DSN is not getting created, but not getting any log or error. How to resolve this ? – learner Dec 18 '18 at 09:44

0 Answers0