Database connection ID

Creating desktop or client-server database solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
jalz
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 340
Joined: Fri Sep 12, 2008 11:04 pm

Database connection ID

Post by jalz » Thu Jun 13, 2013 2:23 pm

Hi Guys,

Getting quite determined to start my database app. I've got a Main stack and within that main stack I have a function which connects to the database and returns a connectionId into a global field called connID. This all occurs on the preopen stack command and is present in the stack script.

I've then created a substack called customer (and I'll create others called Supplier, Timesheets etc), and am evaluating if on preopen (connID>0) populate my datagrid. This is producing an error, so my question is, if I have made a connection in the main script, can I use that connection id in substacks or do I need to create a new connection for each substack.

Or do I have to take it further and create a connection everytime I query the database.

Hope my question makes sense. Really want to get going with the designing the core of my system.

Thanks

Klaus
Posts: 14206
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Database connection ID

Post by Klaus » Thu Jun 13, 2013 2:45 pm

Hi jalz,

## returns a connectionId into a global field called connID
There is no such thing as "global field" ;-)
Is this a FIELD or a (global) variable?

Of course you can access the database from all (sub-)stacks, you could do this:
1. use a GLOBAL variable instead of a field
2. Access that field that holds the CONNECTIONID from the substack:
...
put fld "the one with the connection ID" of cd 1 of stack "your mainstack here" into tConnID
...

Important hint:
## This all occurs on the preopen stack command
This will FAIL in a standalone stack, since the neccessary Livecode libraries (database lib in this case)
have not been loaded at that point! Use "openstack" or "opencard" instead!


Best

Klaus

jalz
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 340
Joined: Fri Sep 12, 2008 11:04 pm

Re: Database connection ID

Post by jalz » Thu Jun 13, 2013 3:29 pm

Hi Klaus,

Apologies, I was in my FileMaker mode (my day job) You're right it was the global variable. I'm storing the connID in a global variable.

Thank you for the hint regarding opencard. I'll see how I get on later on when I am at home. I'll see if I can use the connID from the Main stack to access the records in a substack. if not I'll post my code (which is fairly basic) to get further advice :)

All the best

Klaus
Posts: 14206
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Database connection ID

Post by Klaus » Thu Jun 13, 2013 4:28 pm

Hi jalz,

## I'll see if I can use the connID from the Main stack to access the records in a substack.
Sure you can! 8-)

You only need to declare it in the script or handler everytime before you use it:
---
global gConnID
## Now use it :-)
...


Best

Klaus

jalz
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 340
Joined: Fri Sep 12, 2008 11:04 pm

Re: Database connection ID

Post by jalz » Thu Jun 13, 2013 9:24 pm

Hi Klaus,

Thanks didn't realise I have to declare the global variable in each stack? I'll try and get this to work - so far its not working, probably me missing something as usual.

Secondly, The code for openstack code I have written on my main stack, under stack script seems to be executing when I open a substack. Is this normal behaviour? If it is, then I suppose I need to put the code stack script of the main stack into the card script so it doesnt execute all the time.

Apologies for the newbie questions, trying to get my teeth into live code :D

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Database connection ID

Post by mwieder » Thu Jun 13, 2013 9:49 pm

I suppose I need to put the code stack script of the main stack into the card script so it doesnt execute all the time.
Exactly. That's the preferred way to do things. In fact, the docs on openstack and preopenstack say
Sent to the destination card right after you open a stack.

Post Reply