6 Jul 2011 00:00
postgresql return code
I'm trying to determine if a user is in the database...If they are
not, then I want to add them.
in DB2, the return code is different when a "SELECT" statement returns
a value, or if the result is empty, or if the query failed etc..
But in postgresql, it seems that the return code is 0 if the "SELECT"
statement returns an entry or not, just as long as it doesn't fail.
<code snippet>
# is the user_id in the Database?
psql xxxx -U postgres -c"select * from users where USER_ID = '$user_id';"
result=$?
echo "result from select is: $result"
# if the user is NOT in the Database, then add them.
if [ $result = "1" ] ; then
psql xxxx -U postgres -c"insert into users (last_name, first_name,
user_id, email_adx) values ('$last_name', '$first_name', '$user_id',
'$email_adx');"
fi
</code snippet>
So when I run the script...(which parses a list of users, and checks
the postgres db), the return code is always 0.
[rmckenno <at> CentOS_x64 ~]$ ./pg_update.sh
user_id | last_name | first_name | email_adx
----------+-----------+------------+--------------------------
rmckenno | mckennon | robert | robert.mckennon@...
(Continue reading)
RSS Feed