Cannot get this to move data to SQL

def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	if not initialChange:
	     if currentValue == 1:
	        
	        BottleFeedDown = system.tag.read("[.]R3_Wait_For_Bottles").value
	        FillerDown = system.tag.read("[.]R3_Filler_Down").value
	        CapperDown = "0"
	        LabelerDown = system.tag.read("[.]R3_Labeler_Down").value
	        CasePackerDown = system.tag.read("[.]R3_CasePacker_Down").value
	        PalletizerDown = "0"
	        WrapperDown = "0"
	        MachineNumber = system.tag.read("[.]R3_Machine_Number").value
	        
	        SQL = '''
	        INSERT INTO EquipmentDown
	        (BottleFeedDown, FillerDown, CapperDown, LabelerDown, CasePackerDown, PalletizerDown, WrapperDown,0 ,MachineNumber)
	        VALUE
	        '''
	        
	        result = system.db.runPrepUpdate (SQL, [BottleFeedDown, FillerDown, CapperDown, LabelerDown, CasePackerDown, PalletizerDown, WrapperDown,0, MachineNumber], 'Ignition')

currentValue is a QualifiedValue object. You need to use currentValue.value.

You should also consolidate your individual tag reads into a single call to system.tag.readBlocking(). Single reads are painfully inefficient.

2 Likes

Also, your query needs to have question marks as placeholders for the values.

2 Likes

It should look more like this, but that 0 as a column name looks suspect in SQL.

def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	if not initialChange:
		if currentValue.value == 1:
			tagPaths = ["[.]R3_Wait_For_Bottles", "[.]R3_Filler_Down", "[.]R3_Labeler_Down"], "[.]R3_CasePacker_Down", "[.]R3_Machine_Number"]
			qVals = system.tag.readBlocking(tagPaths)
			
			BottleFeedDown = qVals[0].value
			FillerDown = qVals[1].value
			CapperDown = "0"
			LabelerDown = qVals[2].value
			CasePackerDown = qVals[3].value
			PalletizerDown = "0"
			WrapperDown = "0"
			MachineNumber = qVals[4].value
	        
	        SQL = '''
	        INSERT INTO EquipmentDown
	        (BottleFeedDown, FillerDown, CapperDown, LabelerDown, CasePackerDown, PalletizerDown, WrapperDown,0 ,MachineNumber)
	        VALUES
	        (?,?,?,?,?,?,?,?,?)
	        '''
	        args = [BottleFeedDown, FillerDown, CapperDown, LabelerDown, CasePackerDown, PalletizerDown, WrapperDown,0, MachineNumber]
			result = system.db.runPrepUpdate(SQL, args, 'Ignition')
1 Like

Not sure if that values fill all the columns, but if they do, the query could have been meant as:

	        SQL = '''
	        INSERT INTO EquipmentDown
	        VALUES (?, ?, ?, ?, ?, ?, ?, 0, ?)
	        '''
1 Like

You are correct I am not writing to all the columns and must have something off because of that. Below are the columns of the table.

|id|int
|BottleFeedDown|int
|FillerDown|int
|CapperDown|int
|LabelerDown|int
|CasePackerDown|int
|PalletizerDown|int
|WrapperDown|int
|TimeStamp|datetime
|MachineNumber|nvarchar(50)