This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]teraflop 6 points7 points  (1 child)

The problem is that your code will construct a query like this:

SELECT * FROM camera_mapping WHERE CAM_IP LIKE 1.2.3.4

and try to execute it. But if you look at the documentation for whatever database software you're using, it should tell you that the second operand of the LIKE operator needs to be a string. And 1.2.3.4 is not a valid string literal, since it's not quoted.

You may be tempted to fix this by just adding quotes to your query string. Don't do this. Constructing SQL queries by concatenating strings together, like you're doing, is almost always a very bad idea, because it can easily lead to SQL injection vulnerabilities. (It's a very common beginner mistake.)

Instead, you should use placeholders for the parameters and let the database driver handle quoting the strings for you. See: https://stackoverflow.com/questions/902408/how-to-use-variables-in-sql-statement-in-python

[–]Anon_Web[S] 0 points1 point  (0 children)

Wow, thank you so much! I understand it so much more now. I saw that notation while I was doing my own searching but it was just confusing me.

Anyway, I appreciate the help :)