MS SQL Database connection
Tim Golden
mail at timgolden.me.uk
Mon Mar 5 04:44:31 EST 2007
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>
More information about the Python-list
mailing list