Pygres question 
Author Message
 Pygres question

Thank you for every one who responded to my newbie questions.  I am
making progress.

I am now experimenting with Pygres.  I did not see in the tutorial
examples that came with Pygres95 how to insert something into a table
using variables in the place of the direct values.

The folowing code does not work.  I first tried the 'INSERT INTO'
method, then commented it out and tried "inserttable" but both times
without success.  What am I doing wrong?

---------------------------------------------------------
from pg95ext import *
from pgtools import *
import string
def create_table(cnx):
        cnx.query("CREATE TABLE instansies (naam varchar(30), id int,"\
                       "telefoon varchar(15), skakelkode varchar(8),"\
                       "faksnommer varchar(15))")

def invoer(cnx):
    Vnaam = raw_input('Naam van instansie: ')
    Vtelefoon = raw_input('Telefoonnommer: ')
    Vskakelkode = raw_input('Skakelkode: ')
    Vfaksnommer = raw_input('Faksnommer: ')
    lys = (Vnaam,Vtelefoon, Vskakelkode,Vfaksnommer)
    cnx.inserttable('instansies',lys)
#  cnx.query('INSERT INTO instansies (naam, telefoon, skakelkode,faksnommer)'\
#            'VALUES(lys)')
    cnx.query('SELECT * FROM instansies')
    display(cnx.listfields(), cnx.getresult())

cnx = connect(dbname='jhspies')
create_table(cnx)
invoer(cnx)
cnx.query("DROP TABLE instansies")
---------------------------------------------------------------------------

Johann.

------------------------------------------------
Johann Spies

Windsorlaan 19
Pietermaritzburg
3201
Suid Afrika (South Africa)
Tel/Faks Nr. +27 331-46-1310



Sun, 02 Apr 2000 03:00:00 GMT  
 Pygres question

Quote:

> I am now experimenting with Pygres.  I did not see in the tutorial
> examples that came with Pygres95 how to insert something into a table
> using variables in the place of the direct values.
> The folowing code does not work.  I first tried the 'INSERT INTO'
> method, then commented it out and tried "inserttable" but both times
> without success.  What am I doing wrong?

> ---------------------------------------------------------
> from pg95ext import *
> from pgtools import *
> import string
> def create_table(cnx):
>         cnx.query("CREATE TABLE instansies (naam varchar(30), id int,"\
>                        "telefoon varchar(15), skakelkode varchar(8),"\
>                        "faksnommer varchar(15))")

> def invoer(cnx):
>     Vnaam = raw_input('Naam van instansie: ')
>     Vtelefoon = raw_input('Telefoonnommer: ')
>     Vskakelkode = raw_input('Skakelkode: ')
>     Vfaksnommer = raw_input('Faksnommer: ')
>   lys = (Vnaam,Vtelefoon, Vskakelkode,Vfaksnommer)

You may have better luck by using lys = [(Vnaam,Vtelefoon,
Vskakelkode,Vfaksnommer)]
as inserttable is meant to insert a list of tuples

Quote:
>     cnx.inserttable('instansies',lys)
> #  cnx.query('INSERT INTO instansies (naam, telefoon, skakelkode,faksnommer)'\
> #            'VALUES(lys)')

or you have to construct the query string, so that the query sent to backend
will already have the values as string

QueryTemplate = """INSERT INTO instansies (naam, telefoon, skakelkode,faksnommer)
VALUES('%s','%s','%s','%s')"""
Query = QueryTemplate % lys
print Query          # just to see if it's ok
cnx.query(Query)

or you can get fancy by using the 'get from dictionary' variety of format string :

ValueDict = {}
ValueDict['naam'] = raw_input('Naam van instansie: ')
Value['telefoon'] = raw_input('Telefoonnommer: ')
Value['skakelkode'] = raw_input('Skakelkode: ')
ValueDict['faksnommer'] = raw_input('Faksnommer: ')

InsertQueryTemplate = """INSERT INTO instansies (naam, telefoon, ,faksnommer)
VALUES('%(naam)s','%(telefoon)s','%(skakelkode)s','%(faksnommer)s')"""
Query = InsertQueryTemplate % ValueDict
cnx.query(Query)

SelectQueryTemplate = """SELECT * FROM instansies WHERE
faksnommer='%(faksnommer)s' "
Query = SelectQueryTemplate % ValueDict
res = cnx.query(Query)
print res

And note the quotes around string values

-------
Hannu



Wed, 05 Apr 2000 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. PyGres????

2. Pygres and large-objects

3. PyGres[2.0 | 2.1]

4. pygres, postgreSQL, gadfly - a bit off-topic

5. pygres

6. PyGres fetchone problem

7. Questions-Questions-Questions-Answers-Answers

8. Question(Questions, questions...)

9. Newbie Question: Realbasic Question

10. Questions Questions

11. Questions and more questions...

12. Browse Question / Report question

 

 
Powered by phpBB® Forum Software