Sqlite3 Error: near question mark: syntax error [duplicate]

2024/7/6 21:44:53

I am trying to use placeholders, but they do not work. Here is a bad example of what i need, it works perfectly, but it is not protected against SQL-injections:

`def updateUser(self, user_id: int, **kwargs) -> bool:for arg, value in kwargs.items():try:sql = f"UPDATE user SET {arg}='{value}' WHERE user_id = {user_id};"self.con.execute(sql)except Exception as e:print(e)self.con.rollback()return Falseself.con.commit()return True
`

It works with any data type perfectly. Now the code that i want to use, but it don't work:

`def updateUser(self, user_id: int, **kwargs) -> bool:for arg, value in kwargs.items():try:self.con.execute("UPDATE user SET ?='?' WHERE user_id = ?;", (arg, value, user_id))except Exception as e:print(e)self.con.rollback()return Falseself.con.commit()return True

` This code returns error:

`>>> ud.updateUser(1, nick="test")
Traceback (most recent call last):File "<stdin>", line 1, in <module>File "<path>/inter.py", line 56, in updateUserself.con.execute("UPDATE user SET ?='?' WHERE user_id = ?;", (arg, value, user_id))
sqlite3.OperationalError: near "?": syntax error

`

I've tried every possible way to write this query (brackets, quotes), but it only works with f-string. What am i doing worng?

Answer

The issue is with the syntax of the query. The ? placeholder can only be used for values, not for column names or table names.

You need to specify the column name and value in the query string, not as placeholders. Here's an example:

def updateUser(self, user_id: int, **kwargs) -> bool:for arg, value in kwargs.items():try:sql = "UPDATE user SET {}=? WHERE user_id=?".format(arg)self.con.execute(sql, (value, user_id))except Exception as e:print(e)self.con.rollback()return Falseself.con.commit()return True

This code should work as expected and will also protect against SQL injection attacks.

https://en.xdnf.cn/q/119858.html

Related Q&A

running bs4 scraper needs to be redefined to enrich the dataset - some issues

got a bs4 scraper that works with selenium - see far below: well - it works fine so far: see far below my approach to fetch some data form the given page: clutch.co/il/it-services To enrich the scrap…

Uploading a file in a embed discord.py (not a image)

Im trying to upload a file directly in a embed, I can upload the file but I dont find the way to put it in the embed. What I want is not displaying the file but uploading it so we can download it, is i…

Cannot install psycopg2 on virtualenv

Hi I use manjaro Linux and I tryed to install psycopg2 packge inside virtualenv but it gave errror error: command gcc failed with exit status 1. Then in the console I tryed gcc --version it saidbash: …

how to execute different print function based on the users input

I am a complete beginner to coding and python so It is probably very simple. So my problem is that am learning how to put if and else function based on the users input and i dont know how to connect be…

matplotlib - AttributeError: module numbers has no attribute Integral

I am a newbie to python and i am trying to learn online. I tried importing matplotlib on python 3.6 but i keep getting this error:problem in matplotlib - AttributeError: module numbers has no attribute…

How to extract social information from a given website?

I have a Website URL Like www.example.comI want to collect social information from this website like : facebook url (facebook.com/example ), twitter url ( twitter.com/example ) etc., if available anywh…

Check if string is of nine digits then exit function in python

I have a function in python that returns different output of strings (Text). And I have different parts that I should check for the string and if the string is of nine digits or contains 9 digits then …

How to extract quotations from text using NLTK [duplicate]

This question already has answers here:RegEx: Grabbing values between quotation marks(20 answers)Closed 8 years ago.I have a project wherein I need to extract quotations from a huge set of articles . H…

takes exactly 2 arguments (1 given) when including self [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

scipy.optimize.curve_fit a definite integral function with scipy.integrate.quad

If I have a function that the independent variable is the upper limit of an definite integral of a mathematical model. This mathematical model has the parameters I want to do regression. This mathemati…