pgdb(Python) interfacing with PostgreSQL problems

Started by
7 comments, last by Clash Rocker 20 years, 8 months ago
Hello *, I'm having a bad hair day over pgdb. I'm trying to store information I've gathered from a HTML for using CGI. I then want to store that information in a database. So I'm using the Python interface pgdb (pypgsql.org).

#!/usr/local/bin/python

import pgdb
import cgi
import time

def Initialise():
	_dbuser	= "foo"
	_dbname = "foo"
	_dbsource = "127.0.0.1:foo"
	return (pgdb.connect(dsn=_dbsource, user=_dbuser, database=_dbname))

def AddData(query):
	db = Initialise()
	cur = db.cursor()
	cur.execute(query)
	db.commit()
	cur.close()
	db.close()

# BEGIN
form = cgi.FieldStorage()
for name in form.keys():
	qu = qu + "'%s'," % (form[name].value)

# Get date and time from the server
d = time.strftime("%Y-%m-%d")
t = time.strftime("%H:%M:%S")

tail = "'%s','%s'" % (d, t)
final_qu = SeAnRe.Action("'Submit',", tail, qu)

StoreData.AddData("INSERT INTO house VALUES (%s)" % (final_qu))
 
However my httpd reports:

Premature end of script headers: House.cgi, referer: http://nova/~agh/house.html

Traceback (most recent call last):, referer: http://nova/~agh/house.html

File "/home/agh/public_html/cgi-bin/House.cgi", line 35, in ?, referer: http://nova/~agh/house.html

StoreData.AddData("INSERT INTO house VALUES (%s)" % (final_qu)), referer: http://nova/~agh/house.html

TypeError: not all arguments converted during string formatting, referer: http://nova/~agh/house.html

Any ideas? Thanks in advance [edited by - clash rocker on August 14, 2003 1:23:29 AM]
Advertisement
looks like final_qu doesn''t have a __str__ method. Try doing str(final_qu), it will probably throw the same error. I dont think the problem is the postgresql.
OK. Got the same error when
>>>str(final_qu)but then I put the str() inside the % formatting:>>>StoreData.AddData("INSERT INTO house VALUES (%s)" % (str(final_qu)))


I get the following:

File does not exist: /usr/local/www/data/favicon.icoPremature end of script headers: House.cgi, referer: http://nova/~agh/house.htmlNOTICE:  identifier "'s','ss','s','s','ss','s','s','s','s','ss','s','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','2003-08-14','01:08:34'" will be truncated to "'s','ss','s','s','ss','s','s','s','s','ss','s','N/A','N/A','N/A", referer: http://nova/~agh/house.htmlTraceback (most recent call last):, referer: http://nova/~agh/house.html  File "/home/agh/cgi-bin/House.cgi", line 35, in ?, referer: http://nova/~agh/house.html  StoreData.AddData("INSERT INTO house VALUES (%s)" % (str(final_qu))), referer: http://nova/~agh/house.htmlFile "/home/agh/cgi-bin/StoreData.py", line 16, in AddData, referer: http://nova/~agh/house.html  cur.execute(query), referer: http://nova/~agh/house.htmlFile "/usr/local/lib/python2.3/site-packages/pgdb.py", line 189, in execute, referer: http://nova/~agh/house.html  self.executemany(operation, (params,)), referer: http://nova/~agh/house.htmlFile "/usr/local/lib/python2.3/site-packages/pgdb.py", line 208, in executemany, referer: http://nova/~agh/house.html  raise DatabaseError, "error '%s' in '%s'" % ( msg, sql ), referer: http://nova/~agh/house.htmlpgdb.DatabaseError: error 'ERROR:  parser: parse error at or near ")" at character 224, referer: http://nova/~agh/house.html' in 'INSERT INTO house VALUES (("'s','ss','s','s','ss','s','s','s','s','ss','s','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','N/A','2003-08-14','01:08:34'", 1))', referer: http://nova/~agh/house.html




[edited by - clash rocker on August 14, 2003 1:24:28 AM]
First edit your post and put your errors in source tags instead of code tags. The lines are too long for code tags.

A word about str().
 def str(s): return s.__str__() 

Thats how str() works (of course the real implementation is done in c and raises exceptions on failure).

You are going to need to provide an alternative str function that parses final_qu and outputs a formatted string. Calling "%s"%(foo) is like saying str(foo), so your "%s"%str(foo) is redundant (and doubly wrong since foo.__str__ doesn''t exist).

You need to do something like this:
def qu_2_str(qu):    s=''''    for a in qu.get_each_value:        s+=str(a) #call str on each value instead of the qu instance    return s 


Of course that is just an example.

Dustin
OK.
Sorry about the code tags...I don''t know if it my browser but the source don''t seem to make much a difference.

Anyhow I tried your suggesten and I get and inside the look I get:
AttributeError: ''tuple'' object has no attribute ''get_each_value'', referer: http://nova/~agh/house.html

Let me start over.

You cannot call str() on your qu_final instance. str() is not magic - it only knows how to convert an instance into a string if you teach it how. The usual things you call str on (ints, floats, dicts, lists, etc) all have __str__() methods (that may not be strictly true, but it helps understanding). Thus
>>> [1,2,3].__str__()''[1, 2 ,3]''>>>str([1,2,3])>>> ''[1, 2, 3]'' 

I have never used cgi or postgresql from python, so I don''t know what object your qu_final instance is. From the errors, I can deduce that the object does not have a __str__ method provided. So, to treat your qu_final instance like a string, you need to provide a converter function of some sort and manually call it.

Let me elaborate with a trivial example.
class A:  def __str__(self):    return "string representation of A instance">>> a=A()>>> a<__main__.A instance at 0x0023498>>>> a.__str__()''string representation of A instance''>>> str(a)''string representation of A instance''class B:   #this has no __str__ method, just like your qu and final_qu instances  pass>>> b=B()>>> str(b)''<__main__.A instance at 0x0023498>'' #python provides this as a default for if you don''t provide a __str__ method, but this isn''t what you are looking for, and for some reason str(qu) is failing anyway


I have no idea what the internal format of your qu_final instance is, so you will have to write the converter function yourself. If you don''t know the structure of your qu_final instance, you have to create your sql command a different way. You may wish to look at the postgresql module docs or the cgi module docs (I have no idea how qu and final_qu got created in your code - they arent created in what you showed).

I hope this helped.

Dustin

ps- The source tags make a big diference on this shitty 800x600 resolution monitor
Hmm I''don''t know. I think I''m just getting lost in Python here.

The contents of final_qu would normally be something like:
"INSERT INTO foo VALUES (''1'',''two'',''hello'',''17/Jan/2003'',''End'')"
That''s how the db likes to recieve the info.

From the cgi side of thing I might get 1 and then two. I then add them to the string with the '' and , and the () of course. I can only see that Python is seeing all the ''foo'', crap and thinking it''s an tuple?

I''m having a load of trouble trying to get help from the Python Interface to PostgreSQL (pygresql or pgdb module) group.
quote:Original post by Clash Rocker
Hmm I'don't know. I think I'm just getting lost in Python here.

The contents of final_qu would normally be something like:
"INSERT INTO foo VALUES ('1','two','hello','17/Jan/2003','End')"
That's how the db likes to recieve the info.

It's not the db's 'preference', it's the standard language (SQL) for doing these things in. What you have above is a simple string, but final_qu probably is not a string. The issue here is what type of object it is.

It doesn't help when you cite pypgsql.org (which doesn't exist), use an object called 'SeAnRe' which doesn't appear elsewhere in your code (what is it? search and replace?), and call a function on it (Action) that isn't in the pgdb docs either. (So what is it? What are those parameters? What does it return?)

This isn't really a Python problem as such, it's more that you're not really specifying exactly what you're doing. If you dig into the docs, and find out exactly what objects and methods you're dealing with, I expect things will become clearer quite quickly. (Can I recommend more descriptive variable names when posting to forums too?)

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]

[edited by - Kylotan on August 19, 2003 12:06:02 PM]
OK so I''m not making much sense. Sorry. I was very tired when I made those posts....and also frustrated. Forgive me please.

I shall start again.
The URL I meant to type in was: www.pygresql.org which is home to the Python PostgreSQL interface. I''m using the pgdb module from that package. Well actully I think pgdb is included the PostgreSQL-7.x branch now anyways.

My Problem is using string objects to query the database.
The following I did in the Python interpretator:
>>> import pgdb>>> db = pgdb.connect(dsn="127.0.0.1:foo")>>> cur = db.cursor()>>> str1 = "hello">>> cur.execute("INSERT INTO foo VALUES (%s)" % (str1))

That is where I get the problems.
I get:
Traceback (most recent call last):  File "<stdin>", line 1, in ?  File "/usr/local/lib/python2.3/site-packages/pgdb.py", line 189, in execute    self.executemany(operation, (params,))  File "/usr/local/lib/python2.3/site-packages/pgdb.py", line 208, in executemany    raise DatabaseError, "error ''%s'' in ''%s''" % ( msg, sql )pgdb.DatabaseError: error ''ERROR:  current transaction is aborted, queries ignored until end of transaction block'' in ''INSERT INTO foo VALUES (hello)''


My foo db is simple one table with:
one text,
two text


This topic is closed to new replies.

Advertisement