How to add new rows to a table using Ignition's Pespective Text Field Component

I have an empty SQL table called Plt65_Supply_Room.
I want to create a user interface such that people can add new rows into my table.
How can i manage this?
Below code it what I have on my Configure events srcipt section on mouce click 'ENTER' Button.
Please help

def runAction(self, event):
		
		ItemName = self.getSibling("Item Name").props.text
		Department = self.getSibling("Department").props.text
		Alias = self.getSibling("Alias").props.text
		Notes = self.getSibling("Notes").props.text
		Equipment = self.getSibling("Equipment").props.text
		OEM_PN = self.getSibling("OEM PN").props.text
		Shaw_PN = self.getSibling("Shaw PN").props.text
		
		system.db.runPrepUpdate("INSERT INTO Plt65_Supply_Room ([Item_Name], [Department], [Alias], [Notes], [Equipment], [OEM PN], [Shaw PN]) Values(?,?,?,?,?)",[ItemName, Department, Alias, Notes, Equipment, OEM_PN, Shaw_PN])
		
		self.getSibling("Item Name").props.text = ''
		self.getSibling("Department").props.text = ''
		self.getSibling("Alias").props.text = ''
		self.getSibling("Notes").props.text = ''
		self.getSibling("Equipment").props.text = ''
		self.getSibling("OEM PN").props.text = ''
		self.getSibling("Shaw PN").props.text = ''
		
1 Like

You need to have the same number of question marks as you have items (You only have 5 vs 7).

1 Like

I find it easier breaking it down, easier to read.

	query = "INSERT INTO Plt65_Supply_Room (Item_Name, Department, Alias, Notes, Equipment, OEM PN, Shaw PN) VALUES (?,?,?,?,?,?,?)"
	args = [ItemName, Department, Alias, Notes, Equipment, OEM_PN, Shaw_PN]
	system.db.runPrepUpdate(query, args)
1 Like

I agree, but I don't like trying to count the ? so I let the code do it for me.

args = [ItemName, Department, Alias, Notes, Equipment, OEM_PN, Shaw_PN]
query = """INSERT INTO Plt65_Supply_Room 
		(Item_Name, Department, Alias, Notes, Equipment, OEM PN, Shaw PN) 
		VALUES ({})"""
query.format(','.join(['?'] * len(args)))
system.db.runPrepUpdate(query, args)
4 Likes

Wasn't aware of that one, that's good to know. I cant remember the last time my insert query worked first time, to check the logs to see I was a ? missing.

Thankyou.

1 Like

@lrose thank you for that tip.

1 Like