SourceForge has been redesigned. Learn more.

#2 total newbie using CherryPy & SQLObject


The following code sample seems to only update via
cache, and I don't see the actual record updated nor
inserted in the database when I call 'store'. I turned
on debugging and I can see the SQL statements. I can
attach to MySQL and query it directly via localhost.
select works fine, too. I can see a row I manually
enter via the web form.

Any assistance is appreciated!


from cherrypy import cpg
from Cheetah.Template import Template
from sqlobject.mysql.mysqlconnection import MySQLConnection

conn = MySQLConnection(user='root', db='test',
passwd="", debug=1)

# this is our (only) data class.
class Contact(SQLObject):
_connection = conn
lastName = StringCol(length = 50, notNone = True)
firstName = StringCol(length = 50, notNone = True)
phone = StringCol(length = 30, notNone = True,
default = '')
email = StringCol(length = 30, notNone = True,
default = '')
url = StringCol(length = 100, notNone = True,
default = '')

class ContactManager:
def index(self):
# Let's display a list of all stored contacts.
contacts =

template = Template('''
<h2>All Contacts</h2>

#for $contact in $contacts
[<a href="./edit?id=$">Edit</a>]
#end for

<p>[<a href="./edit">Add new contact</a>]</p>
''', [locals(), globals()])

return template.respond() = True

def edit(self, id = 0):
# we really want id as an integer. Since
GET/POST parameters
# are always passed as strings, let's convert it.
id = int(id)

if id > 0:
# if an id is specified, we're editing an
existing contact.
contact = Contact.get(id)
title = "Edit Contact"
# if no id is specified, we're entering a
new contact.
contact = None
title = "New Contact"

# In the following template code, please note
that we use
# Cheetah's $getVar() construct for the form
values. We have
# to do this because contact may be set to None
(see above).
template = Template('''

<form action="./store" method="POST">
<input type="hidden" name="id"
value="$id" />
Last Name: <input name="lastName"
value="$getVar('contact.lastName', '')" /><br/>
First Name: <input name="firstName"
value="$getVar('contact.firstName', '')" /><br/>
Phone: <input name="phone"
value="$getVar('', '')" /><br/>
Email: <input name="email"
value="$getVar('', '')" /><br/>
URL: <input name="url"
value="$getVar('contact.url', '')" /><br/>
<input type="submit" value="Store" />
''', [locals(), globals()])

return template.respond() = True

def delete(self, id):
# Delete the specified contact
contact = Contact.get(int(id))
return 'Deleted. <a href="./">Return to Index</a>' = True

def store(self, lastName, firstName, phone, email,
url, id = None):
if id and int(id) > 0:
# If an id was specified, update an
existing contact.
contact = Contact.get(int(id))

# We could set one field after another, but
that would
# cause multiple UPDATE clauses. So we'll
just do it all
# in a single pass through the set() method.
lastName = lastName",
firstName = firstName,
phone = phone,
email = email,
url = url)
# Otherwise, add a new contact.
contact = Contact(
lastName = lastName,
firstName = firstName,
phone = phone,
email = email,
url = url)

return 'Stored. <a href="./">Return to Index</a>' = True

def reset(self):
# Drop existing table

# Create new table

# Create some sample data
firstName = 'testfirstName',
lastName = testLastName',
email = '',
phone = '555-1212',
url = '')

return "reset completed!" = True

print "If you're running this application for the first
time, please go to http://localhost:8080/reset once in
order to create the database!"

cpg.root = ContactManager()
cpg.server.start(configFile = 'tutorial.conf')


  • Oleg Broytman

    Oleg Broytman - 2005-05-26

    Logged In: YES

    The code is too big and badly formatted. Please prepare a
    shorter example and attach it as a text file.

  • Oleg Broytman

    Oleg Broytman - 2005-12-19
    • status: open --> closed

Log in to post a comment.