0
如果這是在錯誤的區域,請直接指向我所在的區域 我正在使用Ignition BY inductiveautomation,並嘗試編寫一個腳本,該腳本將基於如果存在條件。 我有一個查詢將插入一個新的記錄到我下面列出的數據庫,但現在我想能夠更新「設置表」,如果它已經存在。SQL如果在腳本中存在
Code:
queryValue1 = event.source.parent.getComponent('MoldName').selectedStringValue
queryValue2 = event.source.parent.getComponent('MachineName').selectedStringValue
queryValue3 = event.source.parent.getComponent('Text Area').text
if system.gui.confirm("Steve! Are you sure you want to Save changes?",
"Save Changes"):
system.db.runPrepUpdate("INSERT INTO setup_sheetlist (MoldName,machineName,Setupsheet) VALUES ('%s','%s','%s')" % (queryValue1, queryValue2 , queryValue3), getKey=1)
這是我的查詢,它在我的數據庫的工作,但我失去的就如何落實到了RunPrepUpdate,
Code:
IF EXISTS(SELECT * FROM setup_sheetlist WHERE moldname='100SCX-3' AND Machinename='MM11')
UPDATE setup_sheetlist
SET setupsheet ='updated value 3'
WHERE moldname='100SCX-3' AND Machinename='MM11'
ELSE
INSERT INTO setup_sheetlist(MoldName,machineName,Setupsheet)
VALUES('100SCX-3','MM11','TRIAL 2')
這是我到目前爲止,這給了我一個錯誤消息
queryValue1 = event.source.parent.getComponent('MoldName').selectedStringValue
queryValue2 = event.source.parent.getComponent('MachineName').selectedStringValue
queryValue3 = event.source.parent.getComponent('Text Area').text
if system.gui.confirm("Steve! Are you sure you want to Save changes?",
"Save Changes"):
system.db.runPrepUpdate("IF EXISTS(SELECT * FROM setup_sheetlist WHERE moldname='?' AND Machinename='?')UPDATE setup_sheetlist SET setupsheet ='?' WHERE moldname='?' AND Machinename='?'" ,[queryValue1,queryValue2,queryValue3,queryValue1,queryValue2])
ELSE
INSERT
INTO
setup_sheetlist (MoldName,machineName,Setupsheet)
VALUES ('%s','%s','%s')" % (queryValue1, queryValue2 , queryValue3))
錯誤
Error: java.lang.Exception: java.lang.Exception: Error executing system.db.runPrepUpdate(IF EXISTS(SELECT * FROM setup_sheetlist WHERE moldname='?' AND Machinename='?')UPDATE setup_sheetlist SET setupsheet ='updated value 3' WHERE moldname='?' AND Machinename='?', [100SCX-3, MM09, 100SCX-3, MM09], , , false, false)
caused by Exception: Error executing system.db.runPrepUpdate(IF EXISTS(SELECT * FROM setup_sheetlist WHERE moldname='?' AND Machinename='?')UPDATE setup_sheetlist SET setupsheet ='updated value 3' WHERE moldname='?' AND Machinename='?', [100SCX-3, MM09, 100SCX-3, MM09], , , false, false)
caused by GatewayException: SQL error for "IF EXISTS(SELECT * FROM setup_sheetlist WHERE moldname='?' AND Machinename='?')UPDATE setup_sheetlist SET setupsheet ='updated value 3' WHERE moldname='?' AND Machinename='?'": The index 1 is out of range.
caused by SQLServerException: The index 1 is out of range.
完全錯誤報告http://pastebin.com/d7Jt7fcr
我懷疑你沒有得到queryValue1&queryValue2的任何值。你可以運行print語句來驗證runPrepUpdate之前的變量是什麼嗎? –
@TabAlleman,在添加if之前,查詢value1&2與insert語句一起工作 – Jad