Python Forum
Need help on how to include single quotes on data of variable string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help on how to include single quotes on data of variable string
#5
Your query is constructed incorrectly. Somewhere online you probably saw something like this:
query1 = "SELECT * FROM stocks WHERE symbol = '%s'" % symbol
query2 = "SELECT * FROM stocks WHERE symbol = '{}'".format(symbol)
Usually when I see a query formed like this I see it preceded by a comment.
# Never do this -- insecure!
query1 = "SELECT * FROM stocks WHERE symbol = '%s'" % symbol
query2 = "SELECT * FROM stocks WHERE symbol = '{}'".format(symbol)
This kind of code leaves you open to an sql injection attack where an unscrupulous user could enter code in place of a valid symbol.

Instead of using string formatting, you should use placeholders and query parameters. In the example below, "stock_symbol" is a variable that references a stock symbol string that I want added to my query.
# Do this instead
query1 = "SELECT * FROM stocks WHERE symbol = ?"
cursor.execute(query1, (stock_symbol, ))
query2 = "SELECT * FROM stocks WHERE symbol = :symbol"
cursor.execute(query2, {"symbol": stock_symbol})
Your query mixed up the bad str.format() method that is susceptible to sql injection attacks with the recommended placeholders and query parameters method.
query2 = "SELECT * FROM trans WHERE PartNum = {}".format(current_partnum)
cursor.execute(query2,(current_partnum))
You use str.format() to replace {} with whatever is referenced by current_partnum. This is potentially dangerous and should be avoided, but his is only the first problem.

Next you use a query parameter when you execute the query. But there is no placeholder to receive the query parameter.

Finally you don't pass the query parameter correctly. This is a tuple (current_partnum, ). This is a vairable surrounded by parentheses (current_partnum). The trailing comma is required when a tuple contains only one itme.
hani_hms likes this post
Reply


Messages In This Thread
RE: Need help on how to include single quotes on data of variable string - by deanhystad - Jan-10-2023, 06:44 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 3 416 Apr-18-2024, 09:55 PM
Last Post: deanhystad
  How to include one script into another? MorningWave 8 665 Mar-21-2024, 10:34 PM
Last Post: MorningWave
  how include a python code in notpad++ plugin akbarza 2 716 Sep-25-2023, 08:25 PM
Last Post: deanhystad
  Replacing String Variable with a new String Name kevv11 2 858 Jul-29-2023, 12:03 PM
Last Post: snippsat
  Regex Include and Exclude patterns in Same Expression starzar 2 889 May-23-2023, 09:12 AM
Last Post: Gribouillis
Video doing data treatment on a file import-parsing a variable EmBeck87 15 3,098 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  python sql query single quote in a string mg24 1 1,151 Nov-18-2022, 08:01 PM
Last Post: deanhystad
  python r string for variable mg24 3 3,074 Oct-28-2022, 04:19 AM
Last Post: deanhystad
  USE string data as a variable NAME rokorps 1 1,032 Sep-30-2022, 01:08 PM
Last Post: deanhystad
  Removing Space between variable and string in Python coder_sw99 6 6,486 Aug-23-2022, 01:15 PM
Last Post: louries

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020