By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,542 Members | 1,434 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,542 IT Pros & Developers. It's quick & easy.

plpython question

P: n/a
I have the following function and I am getting an invalid syntax error
when I try to run it. I have tried the function a number of ways
including with named parameters and non-named parameters using the
args array. I also tried it with a tab at the beginning and without.
I've also tried with the $$ and with single quotes and the double
single quoting all the existing single quotes.

Any help would be greatly appreciated.

create or replace function BatchBalanceStatus(balance int, needed int, freestock int) returns varchar as
$$
if balance < 0:
return 'Unhandled'
elif freestock >= needed:
return 'OK'
else:
return 'Ordered'
$$ language plpythonu

Thank You
Sim Zacks
IT Manager
CompuLab
04-829-0145 - Office
04-832-5251 - Fax
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
I don't know plpythonu but python ;-) As I really understood your
problem you want to return strings. In Pytho return("OK" ) should work ;-)

Hagen

Sim Zacks wrote:
I have the following function and I am getting an invalid syntax error
when I try to run it. I have tried the function a number of ways
including with named parameters and non-named parameters using the
args array. I also tried it with a tab at the beginning and without.
I've also tried with the $$ and with single quotes and the double
single quoting all the existing single quotes.

Any help would be greatly appreciated.

create or replace function BatchBalanceStatus(balance int, needed int, freestock int) returns varchar as
$$
if balance < 0:
return 'Unhandled'
elif freestock >= needed:
return 'OK'
else:
return 'Ordered'
$$ language plpythonu

Thank You
Sim Zacks
IT Manager
CompuLab
04-829-0145 - Office
04-832-5251 - Fax
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #2

P: n/a
I had tried it exactly the way I wrote it in python as a function and it
worked.

Just in case I tried changing it to return ("OK") as you suggested and
got the same error.

Thank You
Sim Zacks
IT Manager
CompuLab
04-829-0145 - Office
04-832-5251 - Fax

__________________________________________________ ______________________________

I don't know plpythonu but python ;-) As I really understood your
problem you want to return strings. In Pytho return("OK" ) should work ;-)

Hagen

Sim Zacks wrote:
I have the following function and I am getting an invalid syntax error
when I try to run it. I have tried the function a number of ways
including with named parameters and non-named parameters using the
args array. I also tried it with a tab at the beginning and without.
I've also tried with the $$ and with single quotes and the double
single quoting all the existing single quotes.

Any help would be greatly appreciated.

create or replace function BatchBalanceStatus(balance int, needed int, freestock int) returns varchar as
$$
if balance < 0:
return 'Unhandled'
elif freestock >= needed:
return 'OK'
else:
return 'Ordered'
$$ language plpythonu

Thank You
Sim Zacks
IT Manager
CompuLab
04-829-0145 - Office
04-832-5251 - Fax
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #3

P: n/a
Thanks for the link.
I had already looked at that page, and it gives basically no help at
all. I think the problem is how Pgadmin handles new lines or something
like that. because I can't get any 2 line program to work.

I also tried the code in PSQL and it worked when I indented manually
(hitting the spacebar 5 times). I tried that in PGAdmin and it also
failed.

Also in PSql it only worked with non-named parameters. So my list was
(int,int,int) and I referred to them as args[0-2]

It looks like plpython support hasn't hit the mainstream yet.

Thank You
Sim Zacks
IT Manager
CompuLab
04-829-0145 - Office
04-832-5251 - Fax

__________________________________________________ ______________________________

Me again ;-)

I do not have a running postgresql with scripting support so i googled
for some ideas ;-)

The first syntax error must arrive while parsing the argument list,
is'nt it. Postgres means Syntax error at "int". The problem is, that you
can not use the function as you do in python. The argument list contains
only the data types: here ( int, int, int ). The arguments are given by
args[0], args[1] and so on. Furthermore the "code" of your function has
to be encapsulates in '...' . So you can not use '...' to encapsulate
strings.

For more Information take a look at:

http://www.signal42.com/pgsql/plpyth...PLPYTHON-FUNCS
Sim Zacks wrote:
I had tried it exactly the way I wrote it in python as a function and it
worked.

Just in case I tried changing it to return ("OK") as you suggested and
got the same error.

Thank You
Sim Zacks
IT Manager
CompuLab
04-829-0145 - Office
04-832-5251 - Fax

_________________________________________________ _______________________________

I don't know plpythonu but python ;-) As I really understood your
problem you want to return strings. In Pytho return("OK" ) should work ;-)

Hagen

Sim Zacks wrote:
I have the following function and I am getting an invalid syntax error
when I try to run it. I have tried the function a number of ways
including with named parameters and non-named parameters using the
args array. I also tried it with a tab at the beginning and without.
I've also tried with the $$ and with single quotes and the double
single quoting all the existing single quotes.

Any help would be greatly appreciated.

create or replace function BatchBalanceStatus(balance int, needed int, freestock int) returns varchar as
$$
if balance < 0:
return 'Unhandled'
elif freestock >= needed:
return 'OK'
else:
return 'Ordered'
$$ language plpythonu

Thank You
Sim Zacks
IT Manager
CompuLab
04-829-0145 - Office
04-832-5251 - Fax
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org



---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #4

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Sim Zacks wrote:
| I have the following function and I am getting an invalid syntax error
| when I try to run it. I have tried the function a number of ways
| including with named parameters and non-named parameters using the
| args array. I also tried it with a tab at the beginning and without.
| I've also tried with the $$ and with single quotes and the double
| single quoting all the existing single quotes.
|
| Any help would be greatly appreciated.

Easier to help if you actually give the error message, and what version
of PostgreSQL you are running might be significant too.

| create or replace function BatchBalanceStatus(balance int, needed int,
freestock int) returns varchar as
| $$
| if balance < 0:
| return 'Unhandled'
| elif freestock >= needed:
| return 'OK'
| else:
| return 'Ordered'
| $$ language plpythonu

create or replace function BatchBalanceStatus(int, int, int) returns
varchar as '
~ balance, needed, freestock = args
~ if balance < 0:
~ return "Unhandled"
~ elif freestock >= needed:
~ return "OK"
~ else:
~ return "Ordered"
' language plpythonu;

Works just fine here on 7.4.5

- --
Stuart Bishop <st****@stuartbishop.net>
http://www.stuartbishop.net/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFBdfmqAfqZj7rGN0oRAiipAJ9X3IoxinVNx/JRwF9OlzSsZMAATQCgh636
b4kuADMg75BBHqaDjV55c+4=
=LMiW
-----END PGP SIGNATURE-----

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #5

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Stuart Bishop wrote:

| create or replace function BatchBalanceStatus(int, int, int) returns
| varchar as '
| ~ balance, needed, freestock = args
| ~ if balance < 0:
| ~ return "Unhandled"
| ~ elif freestock >= needed:
| ~ return "OK"
| ~ else:
| ~ return "Ordered"
| ' language plpythonu;
|
| Works just fine here on 7.4.5

Urgh... minus the ~ characters of course that my mail program helpfully
inserted :-P

- --
Stuart Bishop <st****@stuartbishop.net>
http://www.stuartbishop.net/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFBdfo0AfqZj7rGN0oRAu0DAKCX1RknM3U+iDMAixKrJt QlSMPVIgCfYA5A
YVvTTcARsnzB8EHVVIc1J+8=
=cdg/
-----END PGP SIGNATURE-----

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.