-3

I don't get it ....

i have a few functions that return rows from sqlite tables. Problem is that some sql querys are working while another not. Function that working perfect :

    def selectAll(predstavnik_id):
        q = "SELECT * FROM ulaz WHERE predstavnik_id = (?)"
        cursor.execute(q, str(predstavnik_id))
        rows = cursor.fetchall()
        if rows is None:
            return None
        else:
            return rows 

And the other one that don't return rows:

    def selectStanByUlaz(stan_id):
        q = "SELECT * FROM stan WHERE ulaz_id = (?)"
        cursor.execute(q, str(stan_id))
        rows = cursor.fetchall()
        if rows is None:
            return None
        else:
            return rows

There is no any errors, just won't return rows ! I tried in sqlite studio with same query syntax and it's working just fine What could be a problem ?

Thanks in advance

2
  • 1
    Those are not the same queries Commented Jan 1, 2024 at 13:58
  • Of course that are not the same queries. First one querying table "ulaz" and another one querying table "stan". But the principle are the same ... Commented Jan 1, 2024 at 17:23

2 Answers 2

2

Both of these functions don't do what you expect them to do:

cursor.execute(q, str(predstavnik_id))
cursor.execute(q, str(stan_id))

The second argument to cursor.execute is defined to take a sequence of parameters.

Notice that cursor.execute(q, str(predstavnik_id)) is not executing with str(predstavnik_id) being the only parameter: cursor.execute is defined to take a sequence, and str(predstavnik_id) is a sequence of characters. You probably meant cursor.execute(q, (str(predstavnik_id), )) (notice the tuple), where str(predstavnik_id) is the only element of that sequence.

The other function suffers from the same problem.

Python should raise a RuntimeError if those strings are not exactly 1 character, yet if your id values are small integers (< 10), it will Just Work (tm) without you noticing the bug.

Sign up to request clarification or add additional context in comments.

4 Comments

Good catch. Additionally: you don't even need the call to str.
@Matthias, yes, or maybe no. The code in the question only allures to the fact that the parameters are integers. Also, converting to a string before passing the object to execute forces the parameter-binding to not use any other converter. So it's not the same, and safer to just replicate what Op wrote, in order not to introduce subtle changes in behavior.
Ok thanks, i'll try your solutions, but as i said, the first one working ok and returning rows from table "ulaz". The second one is the problem, it does'nt raise the error but just return None []. empty ...
@zlocko, both functions as shown in the question are wrong in any case. As I said above, if predstavnik_id is a small integer, it will seem to work, because str(5) is the same as ('5', ) when interpreted as a sequence.
0

Sorry guys but it's nothing wrong with code. It's all in my idiotic head.

I put some dummy data in db for testing, but i put it in wrong version. Instead of db in my project, i referenced db (same name) in sqlite studio from older version of my project ... :-) Sorry for wasting your (&my) time

Happy new 2024

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.