2shady4u / godot-sqlite

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

select_rows based on multiple field parameters #173

Closed rslonake closed 3 months ago

rslonake commented 3 months ago

I have an SQLite database integrated into my game. I have tables for player, inventory_items, and player_inventory...

I want to search the player_inventory table based on player_id and item_id (these are columns in the player_inventory table). I am able to get results if I just search based on one field, but when I try to write the code to search based on both fields I get errors...

I have a scene with TextEdit boxes where I enter the player_id and item_id that I want to use as parameters in the database search.

This works and prints out the results based on item_id- print(database.select_rows("player_inventory", "item_id = '" + $InventoryID.text + "'", ["row_number"])) Results - [{ "row_number": 1 }, { "row_number": 29 }, { "row_number": 57 }]

This works and prints out the results based on player_id- print(database.select_rows("player_inventory", "player_id = '" + $PlayerID.text + "'", ["row_number"])) Results - [{ "row_number": 29 }, { "row_number": 30 }, { "row_number": 31 }, { "row_number": 32 }, { "row_number": 33 }, { "row_number": 34 }, { "row_number": 35 }, { "row_number": 36 }, { "row_number": 37 }, { "row_number": 38 }, { "row_number": 39 }, { "row_number": 40 }, { "row_number": 41 }, { "row_number": 42 }, { "row_number": 43 }, { "row_number": 44 }, { "row_number": 45 }, { "row_number": 46 }, { "row_number": 47 }, { "row_number": 48 }, { "row_number": 49 }, { "row_number": 50 }, { "row_number": 51 }, { "row_number": 52 }, { "row_number": 53 }, { "row_number": 54 }, { "row_number": 55 }, { "row_number": 56 }]

But when I try to run a database table search based on both fields, I get errors in the editor and I can't run the scene... I have tried multiple ways, but I get errors, here is an example...

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

The error I get "Error at (17, 91): Expected closing ")" after call arguments... side note, when I count to find space '91', should I count tabs and spaces or only actual characters?

My goal is to pull the row_number field so I can then run an update_rows function on that specific row...

So, my questions:

Any help greatly appreciated...

2shady4u commented 3 months ago

Hello @rslonake There might be something wrong with the query construction. Would it be possible to enable verbose_mode (by putting verbose_mode to VERBOSE) and showing me the actual query string that gets sent to SQLite?

rslonake commented 3 months ago

Since the error is in the editor itself, I cannot run the scene to see the string sent to SQLite... does this make sense. or are you asking for something from the editor itself?

2shady4u commented 3 months ago

This should work:

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

The AND string was not inside of any quotes in your cases.

rslonake commented 3 months ago

Yes, that works. Thank you for the help.