Periodically, I get questions about specific ADO issues.
I'll use this space to put some of those questions and my answers in case someone might find them useful.
(You can ask questions too via the contact page.)
If you found this page from a search engine, you might enjoy the
Python ADO HowTo or the other
Python and Windows articles.
>When I run the following code
>I get these errors. I think I nearly copied your sample, but no go.
>
>Code:
>import win32com.client
>conn = win32com.client.Dispatch(r'ADODB.Connection')
>conn.Open ("Driver={SQL
>Server};Server=pacific;Database=Cronus;Trusted_Connection=yes;")
>strSQL = 'select * from custinfo c where c.customerid = 3367'
>rs = win32com.client.Dispatch(r'ADODB.Recordset')
>rs.open(strSQL, conn, 1, 3)
>
>Error:
>Traceback (most recent call last):
> File "H:\scripts\SQLConn.py", line 8, in ?
> rs.open('[' + rs_name + ']', conn, 1,3)
> File "E:\Python20\win32com\client\__init__.py", line 347, in __getattr__
> raise AttributeError, attr
>AttributeError: open
it looks to me like it is the "o" in "open". instead of "open" use "Open".
While windows isn't case sensitive, it is case aware allowing Python to be
sensitive. This isn't true w/in the SQL statement, but it is true for
anything interpreted by Python.
>[Longish DAO related question culminating in ...]
>Now that you have all the fields, insert these as a single record into
>the table of the Access database using Python DAO.
to open a database for adding records, do something like ...
>>> dbEngine = win32com.client.Dispatch(r'DAO.DBEngine.36')
>>> db = dbEngine.OpenDatabase(db_name)
>>> rs = db.OpenRecordset(rs_name)
then to add a record do domething like
>>> rs.AddNew()
>>> rs.Fields.Item(field_name1).Value = value1
>>> rs.Fields.Item(field_name2).Value = value2
>>> rs.Fields.Item(field_name3).Value = value3
>>> rs.Update()
>I want to display the contents of the database... how can I do that?...
generally, use the structure at step 9
http://www.e-coli.net/pyado.html#9
to move through the db. replace the line
>>> count = count + 1
with something like
>>> print rs.Fields.Item('Field_Name').Value
which will print all of the values of the field.
or try a list of dictionaries ...
>>> l = [] # a list
>>> rs.MoveFirst()
>>> while 1:
... if rs.EOF:
... break
... else:
... d = {} # a new dict for each row
... d['field1'] = rs.Fields.Item('field1').Value
... d['field2'] = rs.Fields.Item('field2').Value
... d['field3'] = rs.Fields.Item('field3').Value
... l.append(d)
... rs.MoveNext()
you can then look at the data like
>>> print l[4]['field2']
which will print the value from field2 in the 5th row. one caveat,
you will see upon printing text data returned from Access, that strings
will look like
>>> u'a string'
rather than
>>> 'a string'
this is due to MS's use of Unicode. note too that
>>> 'a string' != u'a string'
the easiest thing to do, though probably not always the best thing to do,
is to convert everything to python strings. i.e.
>>> s = str(rs.Fields.Item('field3').Value)
as for printing the whole table, assume a table like
--------------------------------------
| ID Number | First Name | Last Name |
======================================
| 12345 | Steve | Smith |
--------------------------------------
| 12346 | Bob | Jones |
--------------------------------------
| 12347 | Sarah | Brown |
--------------------------------------
you could print the whole thing like
>>> fields = ['ID Number', 'First Name', 'Last Name']
>>> rs.MoveFirst()
>>> while 1:
... if rs.EOF:
... break
... else:
... for field in fields:
... print field, rs.Fields.Item(field).Value
... rs.MoveNext()
this would output something like
'ID Number' 12345
'First Name' 'Steve'
'Last Name' 'Smith'
'ID Number' 12346
'First Name' 'Bob'
'Last Name' 'Jones'
'ID Number' 12347
'First Name' 'Sarah'
'Last Name' 'Brown'
>To get a list of the Tables in a DB using ADO you can invoke the
>[ADOX Catalog] (see example #6)
>
>But is it possible with ADO to get a list of all the fields
>belonging to a certain table??
Fortunately, or unfortunately, programming is not about reasoning by
analogy. The ADOX catalog is the wrong object. to get the fields, you
can simply use the RecordSet object.
>>> cn = win32com.client.Dispatch('ADODB.Connection')
>>> rs = win32com.client.Dispatch('ADODB.Recordset')
>>> cn.Open(DSN)
>>> rs.Open('[My_Table]', cn, 1, 3)
>>> for x in range(rsado.Fields.Count):
... print rsado.Fields.Item(x).Name
Notice that this makes sense, too, as you don't want to have to open up
an ADOX Catalog to get information and then open an ADO RecordSet to use
that information. That would be inefficient.
>It seems like python cant decode DATE and CURRECY
>
>Do you know how to solve this 2 problems maybe?
try this. the time format should use your local settings. the currency
is a little trickier and will require your own formatting.
>>> import win32com.client
>>> c = win32com.client.Dispatch('ADODB.Connection')
>>> r = win32com.client.Dispatch('ADODB.Recordset')
>>> DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=C:/db1.mdb;'
>>> c.Open(DSN)
>>> r.Open('[Table1]', c, 1, 3)
>>> x = r.Fields.Item(1).Value
>>> x
<time object at 011720D8>
>>> x.Format()
'03/01/95 00:00:00'
>>> y = r.Fields.Item(2).Value
>>> y
(0, 850000)
>>> z = y[1]/10000
>>> print 'this information costs $%.*f' % (2,z)
this information costs $85.00
>Just wanted to say thanks for the introduction to ADO. I'm
>sure it's saved me from a few more grey hairs.
>Great! Page! you saved my day!
>Excelent Page!!!!!