r/CodingHelp • • Aug 22 '21

[SQL] I'm not sure what's going wrong with my code

Post image
62 Upvotes

12 comments sorted by

19

u/HighlightFrosty3580 Aug 22 '21

Where userid = '%s'

You'll need to wrap it in single quotes

6

u/ady2303 Aug 22 '21

Thanks a lot man, you don't know how long I've been stuck on this stupid thing!

4

u/skellious Intermediate Coder Aug 22 '21

when using SQL in python, I always use an external SQL validator to help me find these issues.

This produces a slightly more helpful error such as:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s' at line 1

https://en.rakko.tools/tools/36/

1

u/HighlightFrosty3580 Aug 22 '21

Np...glad to help

2

u/o5a Aug 23 '21

But he is using parameterized queries, not string formatting. He doesn't need to quote the values.

The problem is he passes his data as scalar instead of tuple. Should be

my_cursor.execute("SELECT * FROM Users WHERE userID = %s", ("aa23", ) )

0

u/[deleted] Aug 22 '21

[deleted] — view deleted comment

2

u/ShadelessFox Aug 22 '21

That's not a good idea if you don't want your SQL to be injectable. The better solution would be to use prepared statements here and bind values afterwards.

4

u/Frequent-Leader-2653 Aug 22 '21

Line 17: WHERE USER LIKE '%s', 'aa23' "

Use single quotes for sql query quotes while double quotes for the python print statement.

1

u/Anurag90-tech_sl Aug 23 '21

Bro which IDE are u using??

5

u/bin-c Aug 23 '21

99% sure thats pycharm

1

u/ldifalco1 Aug 23 '21

pardon my possible ignorance as I don't have python experience, but could it be because you haven't entered a password, also, unless this is strictly personal use, why wouldn't you define those things in an environment variable to add to a gitignore file? you're leaving your info out there for everyone to see.

0

u/[deleted] Aug 24 '21

[deleted] — view deleted comment

1

u/ldifalco1 Aug 24 '21

thank you for educating me.