inserting data into two tables with a keyfield

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

inserting data into two tables with a keyfield

Post by jalz » Wed Jan 04, 2012 11:11 pm

Hi all,

As my previous thread, I've got data from text fields to populate my table. So far, Ive got 3 fields in the Customer table C_ID (Primary - Auto Increment) , FName and SName. I've got another table called Address which includes the following fields C_ID (this is going to be the keyfield that relates back to Customer ) , Address1, Address2 and Zip.

I can insert into the Customer table with the values that have been typed in on my form, but after I've 'committed' the record in the Customer table - C_ID populates/increments - I need to retrieve this and use the value in the Insert for the Address table. Am I right in thinking I need write two insert statements; write the first to the customer table, retrieve C_ID and then write the second insert to the Address or is their a 'funky' way in Livecode that will allow me to quickly write related data in the correct tables quite easily.

If I do need to write two insert statements what command do I need to use to retrieve the C_ID and I suppose it needs to go in after the revExecuteSQL conID, tSQL

Thanks all

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: inserting data into two tables with a keyfield

Post by Mark » Thu Jan 05, 2012 11:46 am

Hi,

If (some) data inserted into one table is supposed to always be inserted into a second table as well, then you should add a trigger to your database that automatically takes care of this whenever your LiveCode app inserts data into the first table.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

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

Re: inserting data into two tables with a keyfield

Post by jalz » Thu Jan 05, 2012 7:50 pm

Hi Mark,

Thanks for replying. I thought database triggers were great at moving the same data from one table to another - in a shadowtable type usage or audits. I wasn't aware I could use a database script trigger in this manor - will do some reading up on this.

Im thinking it maybe easier to do two inserts personally, as thats how Ive done things in the past in other programming environments, I just need to figure out how to retrieve the C_ID once the data has been inserted into the database.

Jalz

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: inserting data into two tables with a keyfield

Post by Mark » Thu Jan 05, 2012 8:24 pm

Hi Jalz,

Most of the time, you'd use revQueryDatabase to retrieve data from e.g. a SELECT statement.

Edit: revQueryDatabase selects records, revDataFromQuery retrieves data.

Best,

Mark
Last edited by Mark on Mon Feb 13, 2012 2:13 pm, edited 1 time in total.
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

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

Re: inserting data into two tables with a keyfield

Post by jalz » Thu Jan 05, 2012 11:50 pm

Hi Mark,

Thanks - figured out that this will get the id i'm looking for:

Code: Select all

put revDataFromQuery("","",conID,"SELECT last_insert_rowid()") into tCustID
Im suprised I dont need to tell it to use which table after the select. Seems to work without it anyway :)

Post Reply