Python Forum
python sql query single quote in a string - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: python sql query single quote in a string (/thread-38739.html)



python sql query single quote in a string - mg24 - Nov-18-2022

Hi Team,

I am extracting sql table data via python , while extracting data , portfolio contains single quote in its records.


hence my second select query is not working. due to single quote in strings portfolio.
how to handle dynamically.

and portfolio = ‘xx_xxxxx_spread_jun’22’ <-------------‘22’ issue here.


select * from customer.table1 where
MarketDate = '2022-06-30-00:00:00'
and portfolio = 'xx_xxxxx_spread_jun'22'


clubbing sql records and header into variable Info.

1) first query.

result = cursor.execute(qr1)
for idx,row in enumarate(result,start=1)
	info = dict(zip(hd,row))

	qry2 = 
		"""select * from {database}.[dbo].[{tbl}] where
		[MarketDate] = '{info[MarketDate]}'and
		[Portfolio] = '{info[Portfolio]}"""


2) second query.
	output2 = cursor.execute(qr2)  <------- unable to execute query2



RE: python sql query single quote in a string - deanhystad - Nov-18-2022

What is the error message? Can you print out qr2 so we can see the query.