Python Source Code: dbwrite.py
#!/usr/bin/env python
import cgi
import textwrap
import os
import connect
import common
mysql_available = False
try:
import MySQLdb
mysql_available = True
except ImportError:
mysql_available = False
#----------------------------------------------------------
# The Web Language Project
# Mark Brautigam
# May-June 2015
# http://www.mixed-up.com/markb/
#----------------------------------------------------------
common.headers1();
print "p#error { border: 1px solid #c00; background-color: #ff8; padding: 0.5em 1em; }"
common.headers2();
print common.sidebarX("python", "dbwrite", "py");
print textwrap.dedent("""\
<div id="content">
<h2>Python: Write to MySQL database</h2>
""")
#----------------------------------------------------
def formHasData (formData) :
#----------------------------------------------------
nData = len(formData)
for i in range (0, nData) :
if (formData[i].strip() != '') :
return True;
return False;
#----------------------------------------------------
def printForm () :
#----------------------------------------------------
print textwrap.dedent("""\
<form name='parksform' action='dbwrite.py' method='POST'>
<fieldset class='db'>
<legend>Add a park</legend>
<table>
<tr><td>Park:</td><td><input type='text' name='site' /></td></tr>
<tr><td>City:</td><td><input type='text' name='city' /></td></tr>
<tr><td>State:</td><td><input type='text' name='state' /> <span>(4 characters max)</span></td></tr>
<tr><td>Latitude:</td><td><input type='text' name='lat' /></td></tr>
<tr><td>Longitude:</td><td><input type='text' name='lon' /></td></tr>
<tr><td></td><td><input type='submit' id='submit' name='submit' value='Add' /></td></tr>
</table>
</fieldset>
</form>
""")
#----------------------------------------------------
# The main Python code is here.
#----------------------------------------------------
if not mysql_available :
print "<p id='error'>The MySQLdb module is not available on this server.</p>"
else :
printForm();
# Get the values from the form
form = cgi.FieldStorage()
site = form.getvalue('site', '')
city = form.getvalue('city', '')
state = form.getvalue('state', '')
lat = form.getvalue('lat', '')
lon = form.getvalue('lon', '')
submit = form.getvalue('submit', '')
# If they submitted the form, but not when first drawing it
#
if (submit == "Add" and formHasData([site, city, state, lat, lon])) :
# special handling for integers
if lat.strip() == '' :
lat = 0;
if lon.strip() == '' :
lon = 0;
if (not common.is_number(lat)) :
lat = 0;
if (not common.is_number(lon)) :
lon = 0;
if (len(state) > 4) :
state = state[0:4];
print "<p>The following data was entered in the data file: </p>";
print "<table class='results'>";
#print "<tr><td>Park</td><td>{}</td></tr>\n".format (site) #Python 2.6+ only
#print "<tr><td>City</td><td>{}</td></tr>\n".format (city) #Python 2.6+ only
#print "<tr><td>State</td><td>{}</td></tr>\n".format (state) #Python 2.6+ only
#print "<tr><td>Latitude</td><td>{}</td></tr>\n".format (lat) #Python 2.6+ only
#print "<tr><td>Longitude</td><td>{}</td></tr>\n".format (lon) #Python 2.6+ only
print "<tr><td>Park</td><td>%s</td></tr>\n" % (site)
print "<tr><td>City</td><td>%s</td></tr>\n" % (city)
print "<tr><td>State</td><td>%s</td></tr>\n" % (state)
print "<tr><td>Latitude</td><td>%s</td></tr>\n" % (lat)
print "<tr><td>Longitude</td><td>%s</td></tr>\n" % (lon)
print "</table>";
# database connection
db = connect.my_connect()
cursor = db.cursor()
#sql = "INSERT INTO parks VALUES ('{}', '{}', '{}', {}, {}, null);".format (site, state, city, lat, lon)
sql = "INSERT INTO parks VALUES ('%s', '%s', '%s', %s, %s, null);" % (site, state, city, lat, lon)
print sql # debug
cursor.execute (sql)
db.commit()
#----------------------------------------------------
print textwrap.dedent("""\
<p><a href='dbread.py'>Show the contents of this database table »</a></p>
<p><a href='source.py?f=8'>Show Python source code »</a>
</div>
""")
common.footers();