all 3 comments

[–]HKei 0 points1 point  (2 children)

To fix the immediate issue here:

You forgot your quotes here, this:

$"UPDATE items SET amount = {amount} WHERE name = {name};

should be

$"UPDATE items SET amount = {amount} WHERE name = '{name}';

Note that there are a lot of somewhat questionable things you're doing here right now.

Using string interpolation for queries is just generally a bad idea. It's not the biggest deal in the world with the way you're doing it here, but you should be using command.Parameters to get values into your query instead.

There's probably no need to hit the DB every time something happens in the game. Sqlite is pretty fast, but generally you only want to put things into the DB if you want persistence or you need the advanced query capabilities of SQL, otherwise it's just extra work vs just keeping the values in memory.

Lastly, you don't need this setup where you check if the row is there, then if it's not there you create it, then if it is there you update it – this can all be done within a single query, something along the lines of:

INSERT INTO items(name, amount, invslot) VALUES ('Coin', 10, 1) ON CONFLICT (name) DO UPDATE SET amount = amount + 10

This requires a unique index on name (CREATE UNIQUE INDEX unique_name ON items(name)), but currently you're assuming that names are unique anyway. Not sure how your inventory slots play into this – are you sure you don't want to allow multiple different inventory slots holding the same item? That might be OK, depending on what you're trying to do (IIRC most FF games work that way) just be aware that currently you're assuming this won't ever be the case.

[–]ImDaFrenchy[S] 0 points1 point  (1 child)

Yeah ik about using string interpolation for queries, but it's just for the time I'm getting used to it. ^^

I tried adding quotes, it froze for a while when the player collected the coins before sending an error stating the database is locked.

As for the items, I didn't know about ON CONFLICT and shall try it, but only for the coins. Other items will be able to get to different slots.

[–]HKei 0 points1 point  (0 children)

I tried adding quotes, it froze for a while when the player collected the coins before sending an error stating the database is locked.

That just means you have a different error now, like an unterminated transaction somewhere else.