how do I get field names from a query
how do I get the names of the fields returned from a SQL query ? ( I would like to generate stuff using the names automatically) I know they must be available because I can use them directly ! I've tried lots of alternatives of the form <dtml-in "sqlListTable(table_name='personnel')"> <dtml-in sequence-item> <P><dtml-var sequence_item> </dtml-in > which give me the values but not the names, and looked through lots of the old msgs. Thanks in advance if anyone can help adam adam.molyneaux@epfl.ch
On Wed, 31 May 2000, Adam Molyneaux wrote:
how do I get the names of the fields returned from a SQL query ? ( I would like to generate stuff using the names automatically) I know they must be available because I can use them directly !
I've tried lots of alternatives of the form
<dtml-in "sqlListTable(table_name='personnel')"> <dtml-in sequence-item> <P><dtml-var sequence_item> </dtml-in >
Ok... someone may have already answered this, but here's my guess. (Yes, this is untried, sorry :) <dtml-in "sqlQueryBlah()"> <dtml-in sequence-item> <dtml-var sequence-key> = <dtml-var sequence-item> </dtml-in> </dtml-in>
which give me the values but not the names, and looked through lots of the old msgs.
I'm certain this was tackled some time in the past, but yeh, searching the list logs can sometime be suprisingly fruitless.
Thanks in advance if anyone can help
glad i could help. (o8
adam adam.molyneaux@epfl.ch
-- Have a better one, Curtis. <dtml-var standard_work_disclaimer>
You need an external method to get the dictionary object that has the field name to column number Here is the external method: --------------------------------- def doPySQL(self, zQry) : tvar1 = 'res=self.'+zQry exec tvar1 return res._schema.items() ------------------------------------------ Here is a dtml document that uses the external method: (getRes is what I call the external method in Zope) This was for proof of idea - it is not pretty ------------------------------------------------------------------- <html><head><title>Test</title></head><body> <dtml-with "_.namespace(t1=getRes('Your_Query_Here()'))"> <dtml-var "_.len(t1)"> Columns<br> <dtml-var "_.str(t1)" html_quote><br><br> <dtml-in t1> <dtml-var "_.str(_['sequence-key'])"> = <dtml-var "_.str(_['sequence-item'])"><br> </dtml-in> <br><br> </dtml-with> </body></html> </dtml-comment> <html><head><title>Test</title></head><body> <dtml-with "_.namespace(t1=Your_Query_Here(),t2=getRes('Your_Query_Here()'))"> <hr> <dtml-in t1> <dtml-in t2 sort=sequence-item> <dtml-var "_['sequence-key']"> = <dtml-var "_.getitem(_['sequence-key'])" html_quote><br> </dtml-in><hr> </dtml-in> </dtml-with> </body></html> --------------------------------------------------------------------------------- ----- Original Message ----- From: Curtis Maloney <curtis@umd.com.au> To: Adam Molyneaux <adam.molyneaux@epfl.ch> Cc: <zope@zope.org> Sent: Tuesday, May 30, 2000 11:31 PM Subject: Re: [Zope] how do I get field names from a query On Wed, 31 May 2000, Adam Molyneaux wrote:
how do I get the names of the fields returned from a SQL query ? ( I would like to generate stuff using the names automatically) I know they must be available because I can use them directly !
I've tried lots of alternatives of the form
<dtml-in "sqlListTable(table_name='personnel')"> <dtml-in sequence-item> <P><dtml-var sequence_item> </dtml-in >
Ok... someone may have already answered this, but here's my guess. (Yes, this is untried, sorry :) <dtml-in "sqlQueryBlah()"> <dtml-in sequence-item> <dtml-var sequence-key> = <dtml-var sequence-item> </dtml-in> </dtml-in>
which give me the values but not the names, and looked through lots of the old msgs.
I'm certain this was tackled some time in the past, but yeh, searching the list logs can sometime be suprisingly fruitless.
Thanks in advance if anyone can help
glad i could help. (o8
adam adam.molyneaux@epfl.ch
-- Have a better one, Curtis. <dtml-var standard_work_disclaimer> _______________________________________________ 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 )
how do I get the names of the fields returned from a SQL query ? ( I would like to generate stuff using the names automatically) I know they must be available because I can use them directly ! The result of the SQL query has a method 'names()' that contains the column names. N.B. This is a method of the result, not a method of the individual rows, so you cannot normally get at it directly while iterating over the result.
I've tried lots of alternatives of the form
<dtml-in "sqlListTable(table_name='personnel')"> <dtml-in sequence-item> <P><dtml-var sequence_item> </dtml-in >
Try: <dtml-let query="sqlListTable(table_name='personnel')" names="query.names()"> <dtml-in query> <dtml-in names> <dtml-let column=sequence-item> <dtml-var column> is <dtml-var "_[column]"><br> </dtml-let> </dtml-in> </dtml-in> </dtml-let> -- Duncan Booth duncan@dales.rmplc.co.uk int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3" "\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure? http://dales.rmplc.co.uk/Duncan
participants (4)
-
Adam Molyneaux -
Curtis Maloney -
Duncan Booth -
Jim Sanford