Top | Previous | Next |
system.db.runPrepUpdate |
Description Runs a prepared statement against the database, returning the number of rows that were affected. Prepared statements differ from regular queries in that they can use a special placeholder, the question-mark character (?) in the query where any dynamic arguments would go, and then use an array of values to provide real information for those arguments. Make sure that the length of your argument array matches the number of question-mark placeholders in your query. This call should be used for UPDATE, INSERT, and DELETE queries.
This is extremely useful for two purposes:
Syntax system.db.runPrepUpdate(query, args, database, tx, getKey, skipAudit) Parameters String query - A query (typically an UPDATE, INSERT, or DELETE) to run as a prepared statement with placeholders (?) denoting where the arguments go. Object[] args - A list of arguments. Will be used in order to match each placeholder (?) found in the query. String database - The name of the database connection to execute against. If omitted or "", the project's default database connection will be used. String tx - A transaction identifier. If omitted, the update will be executed in its own transaction. Boolean getKey - A flag indicating whether or not the result should be the number of rows returned (getKey=0) or the newly generated key value that was created as a result of the update (getKey=1). Not all databases support automatic retrieval of generated keys. Boolean skipAudit - A flag which, if set to true, will cause the prep update to skip the audit system. Useful for some queries that have fields which won't fit into the audit log. Returns Integer - The number of rows affected by the query, or the key value that was generated, depending on the value of the getKey flag. Scope All Examples This example would gather some user entered text and insert it into the database.
userText = event.source.parent.getComponent("TextArea").text + "VALUES (?,?)", [userName, userText])
This code would read a file and upload it to the database
filename = system.file.openFile() # Ask the user to open a file
This example inserts a new user and gives it the 'admin' role. Demonstrates the ability to retrieve a newly created key value.
#get the username/password name = event.source.parent.getComponent('Name').text desc = event.source.parent.getComponent('Description').text building = event.source.parent.getComponent('Building').selectedValue
#insert the value id = system.db.runPrepUpdate("INSERT INTO machines (machine_name, description) " + "VALUES (?, ?)", [name, desc], getKey=1)
#add a row to the user role mapping table system.db.runPrepUpdate("INSERT INTO machine_building_mapping " + "(machine_id, building) VALUES (?, ?)", [id, building]) |