MySQL

Simon Faulkner News at Titanic.co.uk
Sun Jan 2 17:40:26 EST 2000


Yeah!

It works!

Here is the code, please comment and help me to learn how to write
better code.

>>
#!/usr/bin/python
import MySQLdb, cgi, sys

def bail():
  print "Content-type: text/html\n"  
  print "<H3>Error</H3>"
  sys.exit()

def printaddresses():
  db =
MySQLdb.connect(host="localhost",db='DPAdmin',user="root",passwd="topsecret")
  cursor = db.cursor()
  cursor.execute("SELECT * FROM tblIPAddress ORDER BY Name")
  tuple = cursor.fetchall()
  total = len(tuple)
  if total < 1:
    print "No entries"
  else:
    print "Content-type: text/html\n"
    print "<H3>DP ASSIGNED IP ADDRESSES</H3>"
    print "<P>"
    print "<B>"
    print "<table border cellspacing=0 cellpadding=5>"
    print "<tr>"
    print "<th> Company Name </th>"
    print "<th> IP Address </th>"
    print "</tr>"
    for record in range(total):
      Name, IPAddress =  tuple[record]
      print "<tr>"
      print "<td>" + Name + "</td>"
      print "<td>" + IPAddress + "</td>"
      print "</tr>"
    print "</table>"
  db.close()
  print "</B>"
  print "<FORM METHOD=POST
ACTION='http://www.myaddress.co.uk/cgi-bin/ipaddress.py'>"
  print "<P>"
  print "NAME<INPUT TYPE=text NAME=Name SIZE=50 VALUE=''>"
  print "<P>"
  print "IPADDRESS<INPUT TYPE=text NAME=IPAddress SIZE=30 VALUE=''>"
  print "<P>"
  print "<INPUT TYPE=submit NAME=add VALUE='Add the Address'>"
  print "</FORM>"

class FormData:
  def __init__(self, formdict):
    for fieldname in self.fieldnames:
      setattr(self, fieldname, form[fieldname].value)

#MAIN

sys.stderr = sys.stdout
form = cgi.FieldStorage()
if form.has_key("Name") and not form["Name"].value == "":
  NewName = form["Name"].value
  NewIPAddress = form["IPAddress"].value
  SQL = "INSERT into tblIPAddress (Name,IPAddress) VALUES ('" +
NewName + "','" + NewIPAddress + "')"

db=MySQLdb.connect(host="localhost",db="DPAdmin",user="root",passwd="topsecret")
  cursor=db.cursor()
  cursor.execute(SQL)
  db.close
printaddresses()
<<
On 2 Jan 2000 18:54:07 GMT, boud at rempt.xs4all.nl (Boudewijn Rempt)
wrote:

>Simon Faulkner <News at titanic.co.uk> wrote:
>> Confused - I am!
>
>> I have written a lovely little database stored in MySQL that has a
>> Python front end and uses MySQLdb through _mysql on SuSE with MySQL
>> 3.27 - Fine.  All on my Sony laptop development machine.
>
>> Now I am trying to put it onto my RH6 production server I try to build
>> the MySQLdb-0.1.1 and it reports that it needs _mysql.  Where do I get
>> this from?
>
>> If it is part of MySQL then where is it?  I have the client, server,
>> benchmarks and devel loaded.
>
>> Is there a simple overall explanation of databases in Python anywhere?
>
>As far as I know, everything is simply in the MySQLdb-0.1.1 tarball -
>if you were to unpack it, build it (after checking for local variations,
>I had to do some fiddling due to the places SuSE installs stuff in. The
>file _mysqlmodule.c defines the _mysql module, really.
>
>Is this database thing of yours available for looking at? I've noticed
>a lot of questions about MySQL and Python lately, and having worked on
>one or two apps myself, I though about collecting ideas and writing a
>short paper or tutorial on the subject - you know, pinching ideas here
>and there, digesting and then regurgitating the mess, before presenting
>it to the web...
>
>Interesting points in doing stuff with Python and databases are time
>and dates (a perennial problem - even when you're simply working with
>VB or Delphi and Oracle), numerical formats (ditto, especially when,
>after applying the Microsoft millennium patch the number decimal
>conversion is broken), and of course decent design. 

Simon Faulkner



More information about the Python-list mailing list