LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Labview Database: Need help reading last index key from MYSQL DB

Solved!
Go to solution

Hi all,

I'm new to Labview & MySQL actually learning it on the job. Please forgive me if it's a basic, silly question.

So I need to design a project that will update an existing DB in MySQL. The data is dynamic and must update every time a part goes through a machine.

I have this done and I am connecting to the Db and information is going in. The part I'm stuck on is the autogenerated index key in MYSQL. I need to read back the last key generated in the DB to labview for other parts of the project. 

So how can I generate the last key back, here's what I have so far. Thanks so much in advance. 🙂 

0 Kudos
Message 1 of 6
(3,775 Views)
Solution
Accepted by topic author Deja_Labvú

Hi,

 

if the new key is defined as latest key+1 you could simply try something like

select MAX(Index_Key) from ...

else you could sort your select descending by creation timestamp and use the first return value.

 

 

ʍolɟɐʇɐp ʞuıɥʇ
Message 2 of 6
(3,730 Views)

That worked for me thanks so much!

0 Kudos
Message 3 of 6
(3,722 Views)
Solution
Accepted by topic author Deja_Labvú

If you table design includes an auto-index column you could use the SQL-command

SELECT LAST_INSERT_ID();

to get the last auto-index id that you have created.


Greetings, Jens

Kudos are welcome...
Message 4 of 6
(3,718 Views)

This too worked, brilliant!

0 Kudos
Message 5 of 6
(3,713 Views)

Bear in mind that both solutions can yield different results if more than one database user writes to the same table. My solution will give back the auto-index that your connection created (regardless of the table) while crabman's solution will give back the max-index of from one specific table. So if another user inserts data into the same table directly after your insert command you don't get back the index-id of your insert command.

 

Jens

Kudos are welcome...
Message 6 of 6
(3,702 Views)