The query result is a tuple, with two items. The first item in the tuple is a list of dictionaries, which describe each column. These dictionaries have values for 'name', 'type', 'null', and 'width'. These values came from MySQL, as meta-information about the query results. The second item in the outermost tuple contains the actual data. This is a list of rows, each row is a tuple that has a value for each column, in the same order as the column description dictionaries. So yes, the result is a combination of tuple, list, and dictionary. If these types are confusing, I would suggest reading some of the material on www.python.org. Sample query result: id | title ----|--------- 1 | john 2 | greg Written in python as a "result object": result = ( [ {'name': 'id', 'type': 'NUMBER', 'null': 'NOT NULL', 'width': 4}, {'name': 'title', 'type': 'STRING', 'null': 'NOT NULL', 'width': -1} ], [ (1, 'john'), (2, 'greg') ] ) -Randy
-----Original Message----- From: Lee [mailto:lee.reilly@ntlworld.com] Sent: Wednesday, February 21, 2001 4:26 PM To: zope@zope.org Subject: [Zope] quick question - database connections in python methods & return types?
Hi there,
I have a quick question that I need answered concerning the return types of SQL queries executed in Python methods.
I am querying/updating a database using Python methods as follows:
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ # make a database connection string try: db_conn = self.MySQL_database_connection() except Exception: return "A connection to the database could not be made. <a href='./'> Recover</a>."
try: SQL = "some SELECT query" result=db_conn.query(SQL) except Exception: return "The class could not be created.<a href='./'> Recover</a>." -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
When this is executed and I "return result[1]" it outputs the data in a format like:
[(1, 16, 9, 1)]
* return result[0] gives me: [{'name': 'PRACTICALS', 'type': 's', 'null': None, 'width': None}, {'name': 'TUTORIALS', 'type': 's', 'null': None, 'width': None}, {'name': 'ASSIGNMENTS', 'type': 's', 'null': None, 'width': None}, {'name': 'BONUS', 'type': 's', 'null': None, 'width': None}]
- the exact details aren't neccessary here I think. At first I had problems trying to use this data i.e. parse it an store it in an array, list, etc. but I overcome that by by first converting "result" to a string using `result[1]`.
I was just wondering how to classify the original return type? It is *not* a list, tuple, array or combination of either so what is it?
I'm writing a report on my project you see and I can't say that my method returns an *cough* *cough* value ;-)
Thanks very much in advance if you can help.
Cheers,
Lee
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )