Re: [Tutor] Trying to use MySQLdb.cursor
below to print the data returned from my query, but I would like to make labels at the top of the columns. How do I do this dynamically? You shouldn't, it makes your code very vulnarable to changes in the database! Its the same principle as using 'select * from...', a bad idea in production code. And if you know which columns you are selecting you by definition know what labels to use. And another reason why its a bsad idea is that databvase columns often have weird abbreviated names that you don't want to expose users to. Alan G. I am just trying to write code to demonstrate this capability in Python. If I am actually in a position where I have access to the database schema, I would not do so. I agree with your comments. Vicki ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Mysqldb module - where ?????
I am googling for a slackware package containing the Mysqldb module, but all I am coming up with is MySQLdb. I am not hallucinating right? They are indeed two separate modules? Vicki ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Trying to use MySQLdb.cursor
I finally gave up and used MySQLdb to connect to my database. It connects okay, and returns data, but now I have a new question. I use the code below to print the data returned from my query, but I would like to make labels at the top of the columns. How do I do this dynamically? I would like to get the fieldnames as defined by mysql and print them before printing each column. Is there a way to do this? Here is the relevant portion of the code: def getdata(): conn = MySQLdb.Connect( host='localhost', user='user', passwd='password', db='sample',compress=1, cursorclass=MySQLdb.cursors.DictCursor) cursor = conn.cursor() cursor.execute(SELECT computers.comp_location FROM computers, mice WHERE mice.mouse_type = USB AND computers.comp_location like A% AND mice.mouse_comp = computers.comp_id;) rows = cursor.fetchall() cursor.close() conn.close() print ''' table border=1 cellpadding=5 ''' for row in rows: print tr for cell in row: print td %s /td % row[cell] print /tr Thanks for helping me get going. Vicki ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Getting error that calendar is underfined when importing calendar module
Hi all. I am using Python 2.4 on a Slackware Linux box and am having a problem importing the calendar module into a program that I am writing. The code is simple and looks like this: import cgitb, os, sys cgitb.enable() sys.strerr = sys.stdout import cgi import time import calendar print Content-Type: text/html\n\n print print htmlhead/headbody\n\n #Get date variables from datetime import datetime date = datetime.now().date() today= date.strftime(%m %Y) thisyear = date.strftime(%Y) thismonth = date.strftime(%m) #Print calendar for the current month calendar.prmonth(int(thisyear),int(thismonth)) - For some reason that I don't understand, I get an error when I use this code in a cgi way (run the file out of cgi-bin) but not when I type it from the command line. The error I get is this: /var/www/cgi-bin/calendar.py 43 calendar.prmonth(int(thisyear),int(thismonth)) calendar = module 'calendar' from '/var/www/cgi-bin/calendar.py', calendar.prmonth undefined, builtin int = type 'int', thisyear = '2005', thismonth = '03' AttributeError: 'module' object has no attribute 'prmonth' args = ('module' object has no attribute 'prmonth',) The calendar module description says there is a prmonth, and as I said, it works from the command line. Can anyone tell me what I am missing? Vicki ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Getting error that calendar is underfined when importing calendar module
The cgi is importing itself when you 'import calendar'. Try renaming your calendar.py to something else like calendar-cgi.py Kent Thanks. I was almost there, having noticed that dir(calendar) was different when run from the script than in an interactive session. Vicki P.S. Now to parse the data. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Passing data from html to py
I have one last question on this particular script. I am using the following line to print out the post data: for key in form: print %s: %sbr % (key, form[key].value) It works fine except for when the key is to a list object made by the following select statement: select id=prod[] name=prod[] multiple=multiple size=4 option id=MB name=MBMotherboards/option option id=CPU name=CPUProcessors/option option id=Case name=CaseCases/option option id=Power name=PowerPower Supplies/option option id=Mem name=MemMemory/option option id=HD name=HDHard Drives/option option id=Periph name=PeriphPeripherals/option /select AttributeError: 'list' object has no attribute 'value' args = ('list' object has no attribute 'value',) How does one traverse a list object to get the selected data? Thanks for any help. Vicki ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python cgi doesn't get data from html form as expected
I have recently started doing cgi in Python and have run into a problem. I have an html form which has various widgets which accept data. I also have this in that html file: form action=formdata.py method=post formdata.py runs but doesn't seem to contain the data from the form. I'm not sure of the format for the for with regard to FieldStorage btw. Here is the contents of formdata.py: #! /usr/bin/python import cgitb, os, sys cgitb.enable() import cgi print Content-Type: text/html\n\n print htmlhead/headbody form = cgi.FieldStorage() for each in form: print each print form['fname'].value, form['lname'].value #print address:, form['addr'].value() print /body/html -- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python cgi doesn't get data from html form as expected
I haven't done too much web stuff (or much of anything) with python, but I looked into your question a bit (researching other's problems may help me avoid them =)). A quick search brought up this page which seems to have information which may help you. http://gnosis.cx/publish/programming/feature_5min_python.html Hope that helps. Best Regards, Michael Lasky I don't see anything in that page that changes what I am doing. I can get rid of everything that has to do with retrieving data from cgi-FieldStorage, and the code executes fine although I get a blank page. I have revised the code somewhat, but the initial problem of cgi.FieldStorage being empty is still there. I don't get an error now, just an empty page. Here is the revised code: #! /usr/bin/python print Content-Type: text/html\n\n print print htmlhead/headbody\n\n import cgitb, os, sys cgitb.enable() sys.strerr = sys.stdout import cgi form = cgi.FieldStorage() field_list = 'ul\n' for field in form.keys(): field_list = field_list + 'li%s/li\n' % field field_list = field_list + '/ul\n' print field_list print /body/html\n\n ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python cgi doesn't get data from html form as expected
Please post the HTML for the form you are submitting and the HTML from a View Source on the result. Kent Sorry to have bothered the list. I figured out the answer (sort of). I had parse the HTML code through an XHTML parser which removed all the name= leaving id= only. That doesn't work with the cgi.FieldStorage function. I still don't know how to do it with XHTML which doesn't evidently support the name= at all. So, html it is. Thanks, Vicki ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor