Using Indusoft VbSctript to find last insert id in MySql once every five seconds

lunenburger

Member
Join Date
Jul 2008
Location
Summerside
Posts
209
I am using VbScript in Indusoft to insert data into a MySql database.

Immediately after inserting a row, I need to know what that row number is to link another table to that data.

On a similar system, I was using AdvancedHMI and used this VB.Net code:

Code:
            Try
                MysqlConn.Open()
                COMMAND.CommandText = "SELECT Last_insert_id()"
                Dim RunKey = COMMAND.ExecuteScalar()
                TempRunKeyID = RunKey

and that was working perfectly.....

But I can't find a way to do it with VbScript...

I can find the total number of rows, and as long as nothing gets deleted I am using this code:

Code:
'Find Row Number of current Job
Dim sql1
sql1="Select * FROM meererdb.run_summary"
$numCur=$DBCursorOpenSQL("MySqlDB",sql1)
$rownum=$DBCursorRowCount($numCur)
sql1.close
sql1=Nothing
But if the number of rows changes, that won't work...

Any insight would be greatly appreciated
 
Use the function SELECT LAST_INSERT_ID()
Try that, however, if you insert more than one entry before the function I think it returns the first inserted ID of multiple entries.
Not sure in MySql, as it is a long time since I used it.
 
Are you looking for an autoincrementing index?

Also, how does LAST_INSERT_ID know which table is being referred to?

I have done this by inserting a unique guard field sands there using a WHERE to find that record and SELECT the index
 
Indusoft doesn't let you request the ID on insert? Ignition does. I'd bet there is a way. You don't want to use MySQL for this, you want this to be done in the script.
 
Code:
SELECT MAX(ID) FROM table;
Since it autoincrements, this should work more reliably than the $DBCursorRowCount approach i.e. if intermediate rows are deleted, but may give a different number than LAST_INSERT_ID() if

  • the last row is deleted. or
  • multiple rows are added with a single action.
Although I am curios why LAST_INSERT_ID() does not work; that is a server function, and the client only sends the request.


Does it need a semi-colon? what was the error?


I notice that without an argument, it returns a BIGINT UNSIGNED (64-bit) value; with an argument, it return an unsigned integer; maybe VBS can't handle the former?
 
Last edited:
SELECT MAX(ID) AS LastID FROM My_Table.
However, this is MSSQL, arghh... just noticed the last post so forget it.
 
Thank you all for the responses....

I have tried using the following VbScript:

Code:
'Find Row Number of current Job
Dim sql1
sql1="Select MAX(run_key) As LastID FROM meererdb.run_summary"
$numCur=$DBCursorOpenSQL("MySqlDB",sql1)
$rownum=$DBCursorRowCount($numCur)
sql1.close
sql1=Nothing

It seems to work but it is returning the number 1 as the value when it should be 523 (last line in table)

Should I change the command of DBCursorRowCount?

I am new to VbScript and MySql commands so please go easy....

thank you
 
I just tried

Code:
'Find Row Number of current Job
Dim sql1
sql1="Select LAST_INSERT_ID() FROM meererdb.run_summary"
$numCur=$DBCursorOpenSQL("MySqlDB",sql1)
$rownum=$DBCursorRowCount($numCur)
sql1.close
sql1=Nothing

And it works perfectly....

(It's funny, I am positive I tried that before and I couldn't get it to work????)

thanks to all...
 
Last edited:
SELECT ID AS LastID FROM My_Table WHERE ID = @@Identity;

or
select *from My_Table order by Name desc limit 1;

select *from My_Table where Id = @MaxId;

or

set @MaxId = (select max(Id) from My_Table)

select *from My_Table ID, My_Row where Id = @MaxId;

Worth trying ?
 
In MSSQL, I would do something like this:

Code:
SELECT TOP 1 run_key FROM meererdb.run_summary

I haven't used MySQL for some time, but I think the equivalent code would be something like this:
Code:
SELECT run_key FROM meererdb.run_summary ORDER BY run_key DESC LIMIT 1
 
SELECT AUTO_INCREMENT - 1 as CurrentId FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'My_DB' AND TABLE_NAME = 'My_Table'
Auto increment would be the next one so need to minus 1 as above
 
Thank you all for the responses....

I have tried using the following VbScript:

Code:
'Find Row Number of current Job
Dim sql1
sql1="Select MAX(run_key) As LastID FROM meererdb.run_summary"
$numCur=$DBCursorOpenSQL("MySqlDB",sql1)
$rownum=$DBCursorRowCount($numCur)
sql1.close
sql1=Nothing
It seems to work but it is returning the number 1 as the value when it should be 523 (last line in table)

Should I change the command of DBCursorRowCount?

I am new to VbScript and MySql commands so please go easy....

thank you


you were asking for one row (the single max value) and $rownum was correctly set to 1.


What you want was the value of the field in the row, not the number of rows returned.
 
I just tried

Code:
'Find Row Number of current Job
Dim sql1
sql1="Select LAST_INSERT_ID() FROM meererdb.run_summary"
$numCur=$DBCursorOpenSQL("MySqlDB",sql1)
$rownum=$DBCursorRowCount($numCur)
sql1.close
sql1=Nothing
And it works perfectly....

(It's funny, I am positive I tried that before and I couldn't get it to work????)

thanks to all...

Actually you are still retrieving the number of rows.

if you tried any of the following

Code:
sql1="Select NULL FROM meererdb.run_summary"

sql1="Select 0 FROM meererdb.run_summary"

sql1="Select -1 FROM meererdb.run_summary"

sql1="Select 1e+32 FROM meererdb.run_summary"


sql1="Select 'a modnar string' FROM meererdb.run_summary"
you would still get the same answer from $rownum=$DBCursorRowCount($numCur)

You need to figure out how to the the value of the field (i.e. LAST_INSERT_ID()) SELECTed in a row, not the number of rows (via $DBCursorRowcount(...)).
 

Similar Topics

I have been searching for a way to insert a line into a MySql database once every five seconds. I was using Advanced HMI and the timer tick...
Replies
23
Views
6,404
I am working on a project where I need to accumulate the amout of Runtime of a machine. I am presently able to display the length of the "Current...
Replies
0
Views
3,179
I have a project to automate four generator sets. The system will monitor and store the load demand of the factory. Once there's Power outage, the...
Replies
0
Views
64
Adding ethernet equipment to an existing panel that has none. We have some solid ethernet cables coming from other remote cabinets that I plan to...
Replies
3
Views
124
I'm trying to control a device via MODBUS RTU and the ModbusRtuMasterV2_PcCOM in Twincat 3. I've configured a device with the right com port and...
Replies
7
Views
222
Back
Top Bottom