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] 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 ''' ''' for row in rows: print "" for cell in row: print " %s " % row[cell] print "" Thanks for helping me get going. 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
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: %s" % (key, form[key].value) It works fine except for when the key is to a list object made by the following select statement: Motherboards Processors Cases Power Supplies Memory Hard Drives Peripherals 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] Passing data from html to py
I am trying to understand how to pass data back and forth from a form to a python script. I mostly have it working now, but I am unsure about how to find out which variables are being passed from the html. I can see them in the form.list, but I need to iterate over them and print a label for each. Currently the data gets printed out like this: [MiniFieldStorage('fname', 'Vicki'), MiniFieldStorage('lname', 'Stanfield'), MiniFieldStorage('addr', '123 Street'), MiniFieldStorage('pwd', 'sdkfsadf'), MiniFieldStorage('prod[]', 'Cases'), MiniFieldStorage('email', 'on'), MiniFieldStorage('buyDays', '10')] I need something more like this: First Name: Vicki Last Name: Stanfield etc. The code I have so far is this: #! /usr/bin/python import cgitb, os, sys cgitb.enable() sys.strerr = sys.stdout import cgi print "Content-Type: text/html\n\n" print print "\n\n" form = cgi.FieldStorage() if ( os.environ['REQUEST_METHOD'] == 'POST' ): if form.list != []: print form.list else: print "No POST data." elif ( os.environ['REQUEST_METHOD'] == 'GET' ): if form.list != []: print form.list else: print "No GET data." print "\n\n" --- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Passing data from html to py
> > Hi Vicki --- > > Two things: > > Firstly, a there is a difference between /n and \n :-) > > Secondly, remember that all whitespace (including newlines) in HTML is > collapsed > down to a single space. > > The newlines will be there, but your web browser is just ignoring them. A > quick > fix is something like '%s: %s' % (key, form[key].value). > > -- > John. Yeah, thanks, John. That'll teach me to type instead of cutting and pasting. Thanks too for the lesson. 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
[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 "\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 = , calendar.prmonth undefined, builtin 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] 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
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 "\n\n" import cgitb, os, sys cgitb.enable() sys.strerr = sys.stdout import cgi form = cgi.FieldStorage() field_list = '\n' for field in form.keys(): field_list = field_list + '%s\n' % field field_list = field_list + '\n' print field_list print "\n\n" ___ 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: 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 "" form = cgi.FieldStorage() for each in form: print "each" print form['fname'].value, form['lname'].value #print "address:", form['addr'].value() print "" -- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor