Help with ADO and dynamic insert statement...
waldekO
osuchw at ecn.ab.ca
Fri Jun 14 17:36:01 EDT 2002
candiazoo at attbi.com wrote in message
> I am still having problems using dynamic sql statements to perform lookups and
> inserts into a MS Access database and an Oracle database.
--DELETED--
I can not to answer you question directly but I may show you mine
approach to querying using ADO.
To retrive information from database
from win32com.client import Dispatch
conn = Dispatch('ADODB.Connection')
conn.Open(db_source, db_user, db_passwd)
sql = """
select id, fname, lname, address
from contacts
where contact_id = %s
""" %(contactid)
flag, rs = conn.Execute(sql)
# function name is GetRows but really it returns columns
# so one has to swing the results around uzing zip()
cols = rs.GetRows()
if rows:
rows = zip(*cols)
# To insert I would similarily first construct sql statemnt using
string
# formatting and then call conn.Execute(sql)
# For example:
sql = """
insert into other_contacts
(id, fname, lname, address)
values
(%s, '%s', '%s', '%s')
"""
for row in rows:
conn.Execute(sql %row)
Of course this approach is very simple and does not take into account
escaping
single quotes or exotic data types but it is sufficient for my needs
in most cases.
waldekO
More information about the Python-list
mailing list