Django Raw queryerror

Hi guys, i´m performing a bunch of raw querys to a sqlite database(the one who comes with the django). I tested the query in the data base browser and it works like a charm but when i tested it in views give me the “django.db.utils.OperationalError: near “?”: syntax error”

'>  with connection.cursor() as cursor:
>         cursor.execute(
>             "select Count(point.id), areaTable.description " \
>             "from indoorsite_point point " \
>             "INNER JOIN indoorsite_tag as tag " \
>             "ON point.tag_id = tag.id" \
>             "INNER JOIN indoorsite_area as areaTable " \
>             "ON areaTable.id = point.area_id " \
>             "WHERE point.tag_id IN %s AND point.date BETWEEN %s AND %s " \
>             "Group By point.area_id", [np.array(data["users"]), date, endDate]
>         )
>     row = cursor.fetchall()
>     stats.append(row)'

I tried pass the array hardcoded in the string like " WHERE point.tag_id IN (1,2,3)" and give me the following error “django.db.utils.ProgrammingError: Cannot operate on a closed cursor.”

Any suggestion?

No idea if this is the specific issue or not, but I noticed how you have a space on the end of each line except for the second line quoted above.

1 Like

Thanks for the reply, but i already change it but doesnt fix the issue =/

I change sqlite to my sql and the bug go away -.-
But now, every single query i do with cursor.execute(sql, params) give me empty results even SELECT * FROM USER

Can you repost your code block using the three backticks - ` before and after the code instead of the greater than - > sign at the beginning of each line? It’s difficult to verify your indentation with that code posted like this.

Sorry for the late post, i switch sql lite for mysql and the error just disapear. So if anyone want to do raw querys my advice is to use a production database from the beginning