Hello,
I am encountering an error message when running system.db.runPrepQuery with spaces in the values which will be written to SQL cells. Is there a way to run this and still use spaces?
Here is the button script, which works fine as long as the strings don’t contain spaces:
def convertTuple(tup): #converts a tuple into a string
str = ','.join(tup)
return str
def convertList(list): #converts a list into a tuple
return tuple(list)
#this checks to ensure that the key column value is not a duplicate
Key = event.source.parent.getComponent('Field1').text
query = "SELECT Measure_Index FROM Measurement_Data WHERE Measure_Index = ?"
data = system.db.runPrepQuery(query, [Key])
L1 = event.source.parent.getComponent('Label1').text
L2 = event.source.parent.getComponent('Label2').text
L3 = event.source.parent.getComponent('Label3').text
L4 = event.source.parent.getComponent('Label4').text
L5 = event.source.parent.getComponent('Dropdown5').selectedStringValue
L6 = event.source.parent.getComponent('Dropdown6').selectedStringValue
L7 = event.source.parent.getComponent('Label7').text
L8 = event.source.parent.getComponent('Label8').text
L9 = event.source.parent.getComponent('Label9').text
L10 = event.source.parent.getComponent('Label10').text
L11 = event.source.parent.getComponent('Label11').text
L12 = event.source.parent.getComponent('Label12').text
F1 = "'" + event.source.parent.getComponent('Field1').text + "'"
F2 = "'" + event.source.parent.getComponent('Field2').text + "'"
F3 = "'" + event.source.parent.getComponent('Field3').text + "'"
F4 = "'" + event.source.parent.getComponent('Field4').text + "'"
F5 = "'" + event.source.parent.getComponent('Dropdown5').selectedStringValue + "'"
F6 = "'" + event.source.parent.getComponent('Dropdown6').selectedStringValue + "'"
F7 = "'" + event.source.parent.getComponent('Field7').text + "'"
F8 = "'" + event.source.parent.getComponent('Field8').text + "'"
F9 = "'" + event.source.parent.getComponent('Field9').text + "'"
F10 = "'" + event.source.parent.getComponent('Field10').text + "'"
F11 = "'" + event.source.parent.getComponent('Field11').text + "'"
F12 = "'" + event.source.parent.getComponent('Field12').text + "'"
Ls = (L1,L2,L3,L4,L5,L6,L7,L8,L9,L10,L11,L12)
Fs = [F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12]
if data.rowCount == 0:
#if key value is not a duplicate, code continues here
if (F3 == '') or (F4 == '') or (F5 == '') or (F6 == '') or (F7 == '') or (F7 == '') or (F9 == '') or (F12 == ''): #Ensure mandatory fields are filled
errorMessage = "Please fill all fields marked with *"
system.gui.errorBox(errorMessage)
else:
if system.gui.confirm("Are you sure you want to create this new entry? You cannot undo this action."):
Headers = convertTuple(Ls) #converts SqlLabels tuple into string for transaction
SqlValuesList = convertList(Fs) #converts from tuple to list for SQL transaction
system.db.runPrepUpdate("INSERT INTO Measurement_Data (%s) VALUES (%s)" % ((Headers),','.join(SqlValuesList)))
system.gui.closeParentWindow(event)
else:
errorMessage = "The Measure_Index number already exists, this could indicate someone is entering data at the same time as you. Please cancel out and try again."
system.gui.errorBox(errorMessage)
Here is the error from running this script on a button when one of the values contains a space:
Traceback (most recent call last):
File “event:actionPerformed”, line 56, in
java.lang.Exception: java.lang.Exception: Error executing system.db.runPrepUpdate(INSERT INTO Measurement_Data (Measure_Index,LotNumber,SpoolID,LineNumber,Adhesion,Peel Strength,MeasurementNumber,MeasurementValue,Unit,Result,Comment,Initials) VALUES (‘7’,‘1’,‘1’,‘254’,‘Adhesion’,‘Peel Strength’,‘1’,‘1’,‘Test’,‘Test’,‘TestEntry’,‘ABC’), [null], , , false, false)
caused by Exception: Error executing system.db.runPrepUpdate(INSERT INTO Measurement_Data (Measure_Index,LotNumber,SpoolID,LineNumber,Adhesion,Peel Strength,MeasurementNumber,MeasurementValue,Unit,Result,Comment,Initials) VALUES ('7','1','1','254','Adhesion','Peel Strength','1','1','Test','Test','TestEntry','ABC'), [null], , , false, false)
caused by GatewayException: SQL error for "INSERT INTO Measurement_Data (Measure_Index,LotNumber,SpoolID,LineNumber,Adhesion,Peel Strength,MeasurementNumber,MeasurementValue,Unit,Result,Comment,Initials) VALUES ('7','1','1','254','Adhesion','Peel Strength','1','1','Test','Test','TestEntry','ABC')": Incorrect syntax near 'Strength'.
caused by SQLServerException: Incorrect syntax near 'Strength'.
Ignition v8.1.1 (b2020120808)
Java: Azul Systems, Inc. 11.0.7
Thanks for your help.