Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # this code presupposes the following line exists prior, and that the database file is already open, obviously :P
- var db : SQLite
- func update_values(table : String, criteria : Dictionary, values_to_change : Dictionary):
- # This function translates the inputs that are more convenient to provide into the ones that work with the plugin's update_rows method.
- # EXAMPLE ORIGINAL SYNTAX: DB.db.update_rows("'Home Town'", "\"Name of Town\"=\"Maybe Nowhere\" and \"Town ID\"=1", {"'Name of Town'":"Everything"})
- # table: would need ' ' around table names with spaces in them
- # criteria: would need \" \" around column names with spaces in them
- # would need \" \" around cell data strings with spaces in them
- # ALSO: "and" has to be between each criterium
- # values_to_change: would need ' ' around column names with spaces in them
- # does not actually need anything extra around values if there are spaces in them
- # EXAMPLE NEW SYNTAX: DB.update_values("Home Town", {"Name of Town" : "Maybe Nowhere", "Town ID" : 1}, {"Name of Town" : "Everything"})
- var xtable : String = table
- var xcriteria : String = "" # input criteria is dictionary, but output is string, as noted above
- var xvalues : Dictionary = {}
- #formatting xtable
- if " " in xtable:
- if !xtable.begins_with("'"): xtable = "'" + xtable
- if !xtable.ends_with("'"): xtable += "'"
- #formatting xcriteria
- var ckeys : Array = criteria.keys() #array of original keys, acquired
- var cvals : Array = []
- var xckeys : Array = []
- var xcvals : Array = []
- for i in ckeys:
- cvals.append(criteria[i]) #array of original values, acquired
- if i.begins_with("'"): i = i.right(1)
- if i.ends_with("'"): i = i.left( len(i)-1 )
- if !i.begins_with("\""): i = "\"" + i
- if !i.ends_with("\""): i += "\""
- xckeys.append(i) #array of corrected keys, acquired
- for i in cvals:
- if i is String and " " in i:
- if i.begins_with("'"): i = i.right(1)
- if i.ends_with("'"): i = i.left( len(i)-1 )
- if !i.begins_with("\""): i = "\"" + i
- if !i.ends_with("\""): i += "\""
- elif i is bool: #not entirely sure this is necessary, but it can't hurt
- match i:
- true: i = 1
- false: i = 0
- xcvals.append(i) #array of corrected values, acquired
- for k in len(xckeys):
- var add_and = false
- if len(xcriteria) != 0: add_and = true
- if add_and: xcriteria += " and "
- xcriteria += "%s=%s" % [xckeys[k], xcvals[k]]
- #formatting xvalues
- var vkeys : Array = values_to_change.keys() #array of original keys, acquired
- var vvals : Array = []
- var xvkeys : Array = []
- var xvvals : Array = [] #this variable doesn't really need to exist, but for clarity I'm including it
- for i in vkeys:
- vvals.append(values_to_change[i]) #array of original values, acquired
- if " " in i:
- if !i.begins_with("'"): i = "'" + i
- if !i.ends_with("'"): i += "'"
- xvkeys.append(i) #array of corrected keys, acquired
- for i in vvals: #yet again, I don't think this is strictly necessary, but it can't hurt
- if i is bool:
- match i:
- true: xvvals.append(1)
- false: xvvals.append(0)
- else: xvvals.append(i)
- for k in len(xvkeys):
- xvalues.merge( {xvkeys[k] : xvvals[k]} )
- #finally!
- db.update_rows(xtable, xcriteria, xvalues)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement