why the same functions with same querys gives a result while another not in sqlite?

67 views Asked by At

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

There are 2 answers

4
user2722968 On

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.

0
zlocko On

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