MS SQL Database connection
Hitesh
hitesh287 at gmail.com
Mon Mar 5 11:45:25 EST 2007
On Mar 5, 4:44 am, Tim Golden <m... at timgolden.me.uk> wrote:
> Hitesh wrote:
> > Hi currently I am using DNS and ODBC to connect to MS SQL database.
> > Is there any other non-dns way to connect? If I want to run my script
> > from different server I first have to create the DNS in win2k3.
>
> Here are several ways to connect to an MSSQL database w/o
> having to create "DNS" or anything else in win2k3 ;)
>
> There are other ways (the slightly stale MSSQL module
> from Object Craft, for example, which still works fine
> for Python <= 2.3).
>
> TJG
>
> <code>
> def adodbapi_connection (server, database, username, password):
> #
> #http://adodbapi.sf.net
> #
> import adodbapi
> connectors = ["Provider=SQLOLEDB"]
> connectors.append ("Data Source=%s" % server)
> connectors.append ("Initial Catalog=%s" % database)
> if username:
> connectors.append ("User Id=%s" % username)
> connectors.append ("Password=%s" % password)
> else:
> connectors.append("Integrated Security=SSPI")
> return adodbapi.connect (";".join (connectors))
>
> def pymssql_connection (server, database, username, password):
> #
> #http://pymssql.sf.net
> #
> import pymssql
> if not username:
> raise RuntimeError, "Unable to use NT authentication for pymssql"
> return pymssql.connect (user=username, password=password,
> host=server, database=database)
>
> def pyodbc_connection (server, database, username, password):
> #
> #http://pyodbc.sf.net
> #
> import pyodbc
> connectors = ["Driver={SQL Server}"]
> connectors.append ("Server=%s" % server)
> connectors.append ("Database=%s" % database)
> if username:
> connectors.append ("UID=%s" % username)
> connectors.append ("PWD=%s" % password)
> else:
> connectors.append ("TrustedConnection=Yes")
> return pyodbc.connect (";".join (connectors))
>
> </code>
Thank you.
And I yes I meant DSN not DNS (my mistake, thank you for catching
it ;)
hj
More information about the Python-list
mailing list