[Zope] quick question - database connections in python methods & return types?

Randall F. Kern randy@spoke.net
Thu, 22 Feb 2001 09:16:16 -0800


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 =3D
	(
		[
			{'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=20
> python methods
> & return types?
>=20
>=20
> Hi there,
>=20
> I have a quick question that I need answered concerning the=20
> return types
> of SQL queries executed in Python methods.
>=20
> I am querying/updating a database using Python methods as follows:
>=20
> -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
> # make a database connection string
> try:
>     db_conn =3D self.MySQL_database_connection()
> except Exception:
>     return "A connection to the database could not be made. <a
> href=3D'./'>
>     Recover</a>."
>=20
> try:
>     SQL =3D "some SELECT query"
>     result=3Ddb_conn.query(SQL)
> except Exception:
>     return "The class could not be created.<a href=3D'./'> =
Recover</a>."
> -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
>=20
> When this is executed and I "return result[1]" it outputs the=20
> data in a
> format like:
>=20
> [(1, 16, 9, 1)]
>=20
> * 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}]
>=20
> - 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=20
> an array,
> list, etc. but I overcome that by by first converting "result" to a
> string using `result[1]`.
>=20
> 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?
>=20
> I'm writing a report on my project you see and I can't say that my
> method returns an *cough* *cough* value ;-)
>=20
> Thanks very much in advance if you can help.
>=20
> Cheers,
>=20
> Lee
>=20
>=20
> _______________________________________________
> Zope maillist  -  Zope@zope.org
> http://lists.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists -=20
>  http://lists.zope.org/mailman/listinfo/zope-announce
>  http://lists.zope.org/mailman/listinfo/zope-dev )
>=20