[mod_python] mod_python & MySQLdb question: Done

Giampiero Benvenuti giampiero.benvenuti at chiaroscuro.com
Thu Dec 18 11:13:10 EST 2003


Ok, thanks to your help an thanks to the mailing list archives I did 
find the solution (it's working for me, I'm not sure
it's the "right" one):

here is the code:

------------------------------------------------------------------
import MySQLdb, MySQLdb.cursors, mod_python

def readtitle(req):
	req.content_type = "text/html"
	req.send_http_header()
	mydb = MySQLdb.connect(db='S_N', user='giampiero', compress=1, 
cursorclass=MySQLdb.cursors.DictCursor)
	cursor = mydb.cursor()
	stmt = "SELECT news_title FROM news"
	cursor.execute(stmt)
	rows = cursor.fetchall()
	rows = list(rows)
	for row in rows:
		results = (row['news_title']+',<br>')
		req.write(results)
	
	raise mod_python.apache.SERVER_RETURN, mod_python.apache.OK
----------------------------------------------------------------

Thanks to all of you: great mailing list with nice people. I never gona 
go back umiliated to Zope now:)

Giampiero

On Giovedì, dic 18, 2003, at 01:54 Europe/Rome, Giampiero Benvenuti 
wrote:

> Thanks for your prompt answer and your help.
>
> I'm coming from Zope, just got to mod_python a week ago. Please 
> understand my being naïve.
>
> I'm not quite there yet: thanks to your help now I'm able to get all 
> my titles form mysql, but at the
> end of the page (sigh) I get an apache error: "500 Internal Server 
> Error"
> "The server encountered an internal error or misconfiguration and was 
> unable to complete your request..."
>
> Apache is not OK at all.
>
> here is my code:
>
> import MySQLdb, MySQLdb.cursors
> from mod_python import apache
>
> def read(req):
> 	req.content_type = "text/html"
> 	mydb = MySQLdb.connect(db='S_N', user='giampiero', compress=1, 
> cursorclass=MySQLdb.cursors.DictCursor)
> 	cursor = mydb.cursor()
> 	stmt = "SELECT news_title FROM news"
> 	cursor.execute(stmt)
> 	rows = cursor.fetchall()
> 	mydb.close()
> 	for row in rows:
> 		s = row['news_title']
> 		print s
> 	return apache.OK
>
> Thank you guys,
>
> Giampiero
>
>
>
>
>
> On Mercoledì, dic 17, 2003, at 18:40 Europe/Rome, 
> <tpc at csua.berkeley.edu> wrote:
>
>>
>> hi Giampiero, instead of print or echo, you use return or req.write.  
>> I
>> had a similar question here:
>>
>> http://www.modpython.org/pipermail/mod_python/2003-June/003263.html
>>
>> I hope that helps you.
>>
>> On Wed, 17 Dec 2003, Giampiero Benvenuti wrote:
>>
>>> Hello to all of you,
>>>
>>> I'm quite intrigued by mod_python and the difference with "normal"
>>> python cgi...
>>>
>>> for instance: I can't make this great pice of code (Jaroslaw 
>>> Zabiello,
>>> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/171463) wich
>>> works great in my cgi-bin directory, working with mod_python.
>>>
>>> The problem I have is right at the end, when I have to deal with:
>>>
>>>   for row in rows:
>>>      print row['name'], row['txt']
>>>
>>> The question is: how can I translate this last part of code to make 
>>> it
>>> working with mod_python?
>>>
>>> the great code:
>>> ----------------------------------
>>> import MySQLdb
>>> import MySQLdb.cursors
>>>
>>> conn = MySQLdb.Connect(
>>>      host='localhost', user='root',
>>>      passwd='', db='test',compress=1,
>>>      cursorclass=MySQLdb.cursors.DictCursor) # <- important
>>> cursor = conn.cursor()
>>> cursor.execute("SELECT name, sometext txt FROM foo")
>>> rows = cursor.fetchall()
>>> cursor.close()
>>> conn.close()
>>>
>>> for row in rows:
>>>      print row['name'], row['txt'] # bingo!
>>> ------------------------------------
>>>
>>> any help is much appreciated.
>>>
>>> Thanks,
>>>
>>> Giampiero
>>>
>>> _______________________________________________
>>> Mod_python mailing list
>>> Mod_python at modpython.org
>>> http://mailman.modpython.org/mailman/listinfo/mod_python
>>>
>>
>>
>>
>
>
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python
>




More information about the Mod_python mailing list