2shady4u / godot-sqlite

GDExtension wrapper for SQLite (Godot 4.x+)
MIT License
850 stars 76 forks source link

tryong to update a single row based on parameters #175

Closed rslonake closed 3 months ago

rslonake commented 3 months ago

I need to update a specific row based on the row_number, inventory count (quanity from inventory), and some amount to be added or subtracted... I am using TextEdit bixes to enter a specific player_id and item_id for the query...

I am able to identify the appropriate row and count from the inventory basedon the query...

var query_results = database.select_rows("player_inventory", "player_id = '" + $PlayerID.text + "' AND item_id = '" + $InventoryID.text + "'", ["row_number, count"])

I then set the row_number as an integer variable and the count as a seperate interger variable

I then modify the count using a formula... I print out everything it looks correct...

BUT, when I write the line to update_rows I get errors or the wrong stuff gets updated... here are some examples with explanations

this works BUT updates ALL rows in the table with the correct new 'count' value

database.update_rows("player_inventory", "row_number = row_number " , {"count" : updated_count})

this updates the correct row (10) with the correct new 'count' value - but this works because I manually specify the row

database.update_rows("player_inventory", "row_number = 10 " , {"count" : updated_count})

this gives me a "Invalid operands 'String' and 'int' in opertator '+' error.

database.update_rows("player_inventory", "row_number = '" + row_number + "'", {count = updated_count})

this runs but I get a "SQL error: no such function: int" back and nothing gets updated in the database

database.update_rows("player_inventory", "row_number = int(row_number) " , {count = updated_count})

If it helps, here is the entire script...

extends Control

var database : SQLite

Called when the node enters the scene tree for the first time.

func _ready(): database = SQLite.new() database.path="res://Database/angler_database.db"

Called every frame. 'delta' is the elapsed time since the previous frame.

func _process(_delta): pass

func _on_update_data_button_down(): database.open_db()

#concatenate the PlayerID and InventoryID fields to identify row in database.player_inventory
#var item_to_update = $PlayerID.text +str("_") + $InventoryID.text
#search the database to determine how many of the item the player has
var query_results = database.select_rows("player_inventory", "player_id = '" + $PlayerID.text + "' AND item_id = '" + $InventoryID.text + "'", ["row_number, count"])

# works print(database.select_rows("player_inventory", "player_id = '" + $PlayerID.text + "' AND item_id = '" + $InventoryID.text + "'", ["row_number"]))

#results are in the form [{ "row_number": 10, "count": 12 }] 
print(query_results)
#identify the row to update
var row_number
row_number =  int(query_results[0]["row_number"])
print("the row to update is " + str(row_number))

#pulls the value "32" so I can update player_inventory count field
var database_amount =  query_results[0]["count"]
print("the database amount is " + str(database_amount))
#the amount entered in $Quantity field
var change = $Quantity.text
change = int($Quantity.text) #convert change.text to an integer
print("the amount of change is " +str(change))
var updated_count : int
#calculate the player's updated inventory count for this item
updated_count = change + database_amount
print ("the updated_count is " +str(updated_count))
#write update to database
### this updates ALL rows with the correct updated_count - database.update_rows("player_inventory", "row_number =  row_number " , {"count" : updated_count})
database.update_rows("player_inventory", "row_number =   int(row_number) " , {count = updated_count})

And here is the output when I run this... Opened database successfully (E:/Gogot_practice_project/Database/angler_database.db) [{ "row_number": 94, "count": 12 }] the row to update is 94 the database amount is 12 the amount of change is -5 the updated_count is 7 --> SQL error: no such function: int

rslonake commented 3 months ago

This is the line giving me an error.. database.update_rows("player_inventory", "row_number = int(row_number) " , {count = updated_count})


How do I pass back to the database the integer value of the row_number variable?

rslonake commented 3 months ago

So, I was able to solve this on... here is the update_rows line that works...

database.update_rows("player_inventory", "player_id = '" + $PlayerID.text + "' AND item_id = '" + $InventoryID.text + "'" , {count = updated_count})

rslonake commented 3 months ago

Hope this helps someone (or me in the future)