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@CentOS_x64 ~]$ ./pg_update.sh
 user_id  | last_name | first_name |        email_adx
----------+-----------+------------+--------------------------
 rmckenno | mckennon  | robert     | [email protected]
(1 row)

result from select is:  0
 user_id | last_name | first_name | email_adx
---------+-----------+------------+-----------
(0 rows)

result from select is:  0



So how can one determine the outcome of the "SELECT" statement?  ( I
guess I could use mysql instead of postgresql)

I saw one post where they recommended grepping the output, but that
seems silly to me.

Rob.

---------------------------------------------------------------------
Archive      http://marc.info/?l=jaxlug-list&r=1&w=2
RSS Feed     http://www.mail-archive.com/[email protected]/maillist.xml
Unsubscribe  [email protected]

Reply via email to