LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

MySQL SELECT LAST_INSERT_ID() After Inserting Items to Database

Solved!
Go to solution

Hi all,

 

I am attempting to write to two separate tables on a mySQL database. The first table will be a list of customer details, and the second will be a record of transactions. These will be linked by a unique key called customer ID. 

 

I can insert the data into each table but I cannot appear to run the SELECT LAST_INSERT_ID(); statement required to get the unique customer ID key to be used for the transaction table. 

 

I have ran the same statement in PHPMyAdmin and this runs OK, inserting into the customer table and then returning the customer ID key which could then be used in the transaction table. 

 

I have also tried running the Select statement as a separate execute statement but this also does not work. 

 

Has anyone been able to perform this action through LabVIEW or are there any known workarounds to return the unique key after inserting into a table?

 

Thanks.

 

Scot07

0 Kudos
Message 1 of 8
(3,267 Views)

I think the MySQL driver has an option to enable multiple statements in the same query.  Check to see if this option is enabled.

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
0 Kudos
Message 2 of 8
(3,259 Views)
Solution
Accepted by topic author Scot07

DSN = "Driver=MySQL ODBC 5.2w Driver; SERVER=localhost; UID=root; PWD=pwd; DATABASE=default_db; PORT=3306; BIG_PACKETS=1; MULTI_STATEMENTS=1"

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
0 Kudos
Message 3 of 8
(3,251 Views)

Thanks,

 

I have now enabled this and no longer getting an error being returned from the query.

 

However, I am not getting any recordset data being returned from this query. I would have expected the customer ID key to be returned. 

 

I have moved to the second recordset (I imagine the first is for the INSERT) and getting a single element back but no value. If I change the SELECT statement to return some other data, it returns this OK but is not returning anything on the SELECT LAST_INSERT_ID(); statement. 

 

Thanks,

 

Scot07

0 Kudos
Message 4 of 8
(3,189 Views)

Hi Scot07,

 

how is the column "Customer ID" from your table "Customers" defined within MySQL?

 

It should be AutoIndexing, so that your command SELECT LAST_INSERT_ID() gives a result. It is also common to NOT include a AutoIndex-Column in an Insert-Command. I am very astonished that your Insert "NULL" for that column does not throw an error.

 

Regards, Jens

Kudos are welcome...
0 Kudos
Message 5 of 8
(3,175 Views)

Are you really storing the cost as a string?  and why do you insert the customerID into the transactions table as a string?  

 

Why not do it this way:

 

INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country) VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');

INSERT INTO Transactions (CustomerID, Item, Cost) VALUES (LAST_INSERT_ID(), 'Bread', 1.50);

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
0 Kudos
Message 6 of 8
(3,161 Views)

I have figured out what the issue was - turns out the data was being returned correctly but I had to convert the variant to data for it to be displayed. 

 

The first column is auto-indexing so that was not an issue. I've always inserted into the table with column 1 as NULL with no issues. 

 

The data in this is just an example. I cannot disclose the real data for obvious reasons. 

0 Kudos
Message 7 of 8
(3,125 Views)

I'm glad you figured it out but I think my last post is the better way to handle this.  There is no need to query the ID back into Labview only to send it back to the database in the form of another query.  Send both queries at the same time.  

aputman_0-1623265007337.png

 

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
0 Kudos
Message 8 of 8
(3,107 Views)