Transfer data from webpage form to database

Jerry Sievers jerry at jerrysievers.com
Tue Nov 23 15:03:10 EST 2004


"Pete....." <helten0007 at yahoo.com> writes:

> Hi, yeah It's try I want to change it to a python.cgi.script. And thanks for 
> your input.....
> 
> I use the api pgsql
> So the first thing I do is to import the following and connect to the 
> server:
> 
> #!/pack/python-2.3.2/bin/python2.3
> from pyPgSQL import PgSQL
> import cgi
> import cgitb
> cgitb.enable()
> 
> form = cgi.FieldStorage()  ( I added this )
> connect = PgSQL.connect(user="vvvvv", password="zzzzz", host="xxx.xxx.xx", 
> database="yyyyy")
> 
> Do I then have to write:
> 
> PgSQL.query(''' INSERT into form
>                          Select persons.idpersons, person.username, 
> persons.surname, phone.phone  (And so on ) ( its what the specific tables 
> are called in my database)
>                          From persons, phone                             ( 
> And so on )
>                          Where persons.name = 'Name'%s
>                           AND persons.surname ='Surname'%s
>                           AND persons.username = 'Username'%s ''' 
> (And so on with the rest)
>                            % (form['name'].value, form['surname'].value, 
> form['username'].value))
> 
> I dont think I get it quite right, any help would be highly appreciated. 
> Thanks for all help...

It looks to me like the part here that you don't understand is how to
interpolate your form data into the string for the database query.

You should study up on the % string formatting operator in the Python
docs.

I'll throw you a bone here;

"""
%s %s %s
""" % (form[key1].value, form[key2].value, form[keyN].value)

Each of those %s tokens is going to be replaced with one of the values
in the tuple right of the % operator.

If you want to pile the form values into a dict and then use symbolic
names in the string...

dict = {}
for key in form:
	dict[key] = form[key].value

"""
%(key1)s %(key2)s...
""" % dict

Maybe there is a function in the cgi module that will make the dict
for you?  I am not sure.

Have fun!


-- 
-------------------------------------------------------------------------------
Jerry Sievers   305 854-3001 (home)     WWW ECommerce Consultant
                305 321-1144 (mobile	http://www.JerrySievers.com/



More information about the Python-list mailing list