[Tutor] Tutor Digest, Vol 39, Issue 60
Leon Keylin
ethics at gmail.com
Wed May 23 17:48:49 CEST 2007
Janos, I tried the ODBC way and it worked! I was about to give up and do it
in C#
or some other language but I love Python so much I wanted it to work.
Thank you SO MUCH!
And thanks to all of you who helped as well!
You folks are the best.
>
> Message: 6
> Date: Wed, 23 May 2007 08:07:35 +0200
> From: J?nos Juh?sz <janos.juhasz at VELUX.com>
> Subject: Re: [Tutor] MSSQL Connection
> To: tutor at python.org
> Message-ID:
> <
> OFD30D5C16.F0DCE181-ONC12572E4.001F1BB3-C12572E4.0021A6AC at velux.com>
> Content-Type: text/plain; charset="US-ASCII"
>
> Hi Leon,
>
>
> > > Hi John,
> > >
> > > Here's the code (I do have permissions to truncate, works manually
> under
> > > the same user.
> > >
> > >
> > > import pymssql
> > >
> > > con = pymssql.connect
> > > (host='server',user='user',password='pwd',database='DB_QA')
> > > cur = con.cursor()
> > >
> > >
> > > query="truncate TABLE bbConsolidatedMessageAcks"
> > > cur.execute(query)
> > > print "Deleted Records: %d" % cur.rowcount
> > >
>
> I use two alternatives to connect to mssql server.
>
> ADODB connection via win32com.client
> ------------------
> import win32com.client
>
> cn =win32com.client.Dispatch('ADODB.connection')
> cn.Provider='sqloledb'
> cn.Open('Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security
> Info=False;Initial Catalog=production;Data Source=036fefersqls001')
> # The argument of cn.Open(), the connection string can be created very
> easy on windows.
> # Create a conn.udl on your desktop
> # Open it with double click, and build a new connection string, test it,
> save it
> # Copy and paste from your .udl
>
> stockcode = '100701-06'
> sql = "select * from stocks where stock = '%s'" % stockcode
>
> print sql
> rs = win32com.client.Dispatch('ADODB.recordset')
> rs.Open(sql, cn)
> print rs.Fields[0].Value
> rs.Close()
>
> cn.Close()
> -----------------
>
> But I usually use ODBC with this kind of class.
>
> import dbi, odbc
>
> class myDB:
> def __init__(self):
> """Open a new connection."""
> self.cn =
> odbc.odbc('DSN=myDB;UID=query;PWD=query;DATABASE=myDB')
>
> def closeDB(self):
> """Close the connection."""
> self.cn.close()
>
> def Execute(self, sql):
> cr = self.cn.cursor()
> cr.execute(sql)
> cr.close()
>
> def Query(self, sql):
> try:
> cr = self.cn.cursor()
> cr.execute(sql)
> self.colnames = [field_prop[0] for field_prop in
> cr.description]
> self.result = cr.fetchall()
> self.rownum = len(self.result)
> return self.result
> except:
> self.colnames = [None]
> self.result = [[None]]
> self.rownum = 0
> return [[None]]
>
> test = myDB()
> print(test.Query('Select * from testtable') )
>
>
> Best regards,
> Janos
>
>
>
> ------------------------------
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
> End of Tutor Digest, Vol 39, Issue 60
> *************************************
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070523/ccfae532/attachment.html
More information about the Tutor
mailing list