MSSQL LIKE and IN statements in ADO problem

gregarican greg.kujawa at gmail.com
Thu Jan 19 14:22:43 CET 2006


The IN statement logic is a good mind exercise if there are multiple
parameters that needed to be brought in. Below is the code that fixed
the LIKE statement logic where you needed an ADO parameterized query
used. Apparently the percent signs don't have to be referenced anywhere
in the code, as my tests ran successfully without them:

-------------------------------------------------
import win32com.client
from adoconstants import *


conn = win32com.client.Dispatch(r'ADODB.Connection')
conn.ConnectionString = "Driver={SQL
Server};Server=(local);Database=myDB;Trusted_Connection=yes;"
conn.Open()
if conn.state == adStateOpen:
	print "Connected to database..."
else:
	print "Not connected!"
	exit
cmd=win32com.client.Dispatch(r'ADODB.Command')
cmd.ActiveConnection=conn

name = '@fname'
value = 'raj'
param=cmd.CreateParameter(name, adVarChar, adParamInput, 200, value)
cmd.Parameters.Append(param)
cmd.CommandText = "SELECT first, last FROM myTable WHERE first like ?"
cmd.CommandType = adCmdText
(rs, dummy)  = cmd.Execute()
rowCount = 0
while not rs.EOF:
	print rs.Fields('first').Value, rs.Fields('last').Value
	rowCount=rowCount+1
	rs.MoveNext() 
print "%s records returned." % rowCount
rs.Close()




More information about the Python-list mailing list