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

I have tried using the following code:

Code:
'Find Row Number of current Job
Dim sql1,LII
sql1="Select LAST_INSERT_ID() FROM meererdb.run_summary AS LII"
$numCur=$DBCursorOpenSQL("MySqlDB",sql1)
$rownum=$DBCursorGetValue($numCur, LII)
$DBCursorClose($numCur)

It returns a value of zero when it should be 573

I feel like this should work.... but it doesn't

What am I missing?? (other than better VbScript skills)
 
I have tried using the following code:

Code:
'Find Row Number of current Job
Dim sql1,LII
sql1="Select LAST_INSERT_ID() FROM meererdb.run_summary AS LII"
$numCur=$DBCursorOpenSQL("MySqlDB",sql1)
$rownum=$DBCursorGetValue($numCur, LII)
$DBCursorClose($numCur)
It returns a value of zero when it should be 573

I feel like this should work.... but it doesn't

What am I missing?? (other than better VbScript skills)


$rownum=$DBCursorGetValue($numCur, "LII")
 
this: http://kbase.icbconsulting.com/home/indusoft

you want some form of $DBCursorGetValue e.g. summat like this:
Code:
sql1 = "SELECT LAST_INSERT_ID() AS LII" 
 
$numCur=$DBCursorOpenSQL("MySqlDB",sql1)

$LII = $DBCursorGetValue($numCur, "LII")

$DBCursorClose(numCur)

Hi DrBitBoy,
I want to thank you for trying to help me, and apologize for my lack of understanding of VbScript......

So, do I need to specify the column name and table that I want the value from?

The auto-incrementing column name is run_key

I have tried changing LII in the code to run_key and it doesn't return anything. ($rownum doesn't change value)

Thank you again for the help....
 
Ah, got it. Maybe.


From https://dev.mysql.com/doc/refman/8.0/en/information-functions.html#function_last-insert-id:


xxx.png


The key phrase there is per-connection.



What that means is that you have to do the [SELECT LAST_INSERT_ID()] in the same connection, that is

  • after the INSERT in a database connection
  • AND
  • before closing that database connection i.e. the database connection that did the INSERT.
Is it possible you are issuing the [SELECT LAST_INSERT_ID()] query on a new database connection, separate from the database connection that did the INSERT, or perhaps after closing and re-connecting to the same database connection?
 
Well I couldn't get Select Last_Insert_ID() to work in Indusoft VbScript.
It always returned a zero no matter how I tried to configure it.

So I had to go with Select Max() and it works.

This is the code that seems to work best for me:

Code:
Dim sql0, RecordId,sqlD, tmpResult
sql0 = "Insert into mdb.run_summary (machine_id) values ('" & $Getcomputername() & "')"
$DBExecute ("MySqlDB",sql0) 

sqlD="SELECT MAX(run_key) as RecordId FROM mdb.run_summary;"
tmpResult = $DBCursorOpenSQL ("MySqlDB",sqlD) 

$rownum = $DBCursorGetValue(tmpResult,"RecordId")

DrBitBoy, thanks for all the help!!
 
I wouldn't use MAX unless there is an index, but since this is your primary key should be by default. You could also order by 'run_key' desc and limit/use top of 1.

Did you ever contact Indusoft? Just hard to believe they don't have this built-in where you can request the key from the initial query.
 
...
This is the code that seems to work best for me:
...

Cool beans. Gotta love reverse engineering. Since is are only ever adding one record, and letting the INTEGER AUTO_INCREMENT field be auto-incremented, that should always work.

For the record, and I just remembered this, it may (may) be possible to send multiple queries over one connection in one SQL string using the MySQL semi-colon separator e.g.

Code:
sql0 = "INSERT INTO [...] [COLOR=Red][I][B];[/B][/I][/COLOR] SELECT LAST_INSERT_ID()"
...
See the attachment, which is admittedly from a Linux command line using the mysql client; processing multiple queries my not be built into the AVEVA $DB.. API.
 

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,405
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