Postgres Connection

Creating desktop or client-server database solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Philhold
Posts: 64
Joined: Thu Mar 05, 2009 3:39 pm

Postgres Connection

Post by Philhold » Sat Mar 07, 2009 1:13 pm

I'm taking a look at Postgresql and have adapted the script in the "Work with Databases" stack to connect my stack to a test database. The following snippet does if fact connect but the connection test "if it is not true then" throws an error. It also throws an error if I use the integer test that works on SQLite and MySQL.

I've done some searching for a solution but can't find one.

Any ideas on how to check that the connection has actually been made to Postgresql will be gratefully received. Otherwise I will remove the test and error dialogue and rely on the fact that a SELECT does in fact select a set.

Thanks

Phil

Code: Select all

-- Connect to the database.
on databaseConnect
   get revOpenDatabase("postgresql", "127.0.0.1:5432", "address_book", "postie", "Bob")   
     if it is not true then
    error "Could not connect to database:" && it
  end if
  put it into sDatabaseId
  showRecord
end databaseConnect

Lynn P.
Posts: 79
Joined: Sun Apr 09, 2006 1:09 pm

Post by Lynn P. » Sat Mar 07, 2009 4:45 pm

Hi Phil,

I'm no expert, but I believe the revOpenDatabase returns an integer which is an ID to the database, and your current code is checking for "true", a boolean. If revOpenDatabase fails, it returns an error which isn't an integer, so you can check the connection by verifying the return as an integer or not rather than "true".
Example:

Code: Select all

if theResult is a number then
    put theResult into fld "dbase ID"
  else
    answer theResult
end if

Philhold
Posts: 64
Joined: Thu Mar 05, 2009 3:39 pm

Post by Philhold » Sat Mar 07, 2009 5:25 pm

Hi Lynn,

Many thanks for taking the trouble to reply.
I've incorporated part of your code into my connection code like this.

Code: Select all

on databaseConnect
   get revOpenDatabase("postgresql", "127.0.0.1:5432", "address_book", "postie", "Bob")   
            put it into fld "dbase ID"
  showRecord
end databaseConnect
and as you say I get an integer which seems to be an incrementing number. ie when I connect again I get a number that is one higher.

I think that I understand a bit more now and will try and figure out why I'm getting an incorect error throw.

Thanks again!

Phil

Lynn P.
Posts: 79
Joined: Sun Apr 09, 2006 1:09 pm

Post by Lynn P. » Sat Mar 07, 2009 5:46 pm

Hi again Phil,

I obviously didn't have enough coffee yet this morning because I missed the line in your original post which said "It also throws an error if I use the integer test that works on SQLite and MySQL."
So you're saying you still get your error line: "Could not connect to database:" even if you connected and got a database ID returned?

If you use the entire code below and you get connected but it still throws up the answer dialog, what is theResult that is displayed in dialog?
What ever is in "theResult" will come directly from Rev and give you a clue.

Code: Select all

on databaseConnect 
   get revOpenDatabase("postgresql", "127.0.0.1:5432", "address_book",  "postie", "Bob") 
  if theResult is a number then -- successful connection
    put theResult into fld "dbase ID" 
    showRecord
  else 
    answer theResult -- error, no connection
  end if    
end databaseConnect
And yes, each time you connect to the database, the database ID will increment. Each connection session is unique, hence the automatic incrementing.

Post Reply

Return to “Databases”