0

I am using the Pandas library in Python with the Spyder application to import data from a SQL server. The import requires the following construct:

conn=pyodbc.connect('Driver=SQL Server;'
'Server=WB-SB-SQL-3,1542;'
'Database=WQI;'
'Trusted_Connection=yes;')

Finding2021=pd.read_sql_query(' SELECT' 
' ID, '
' SYSTEM_NO, '
' SOURCE_NO, '
' SAMP_DATETIME, '
' STORE_NUM '
' FROM WQI.DBO.Finding '
' WHERE SAMP_DATE >= \'20110101\'' 
,conn)

I am adding the single quote and space to both sides of each line of the embedded SQL script, manually (see below).

' 

Is there a way to add the single quote and space in bulk--let's say I have a 100-line SQL script--I don't want to have to type the single quotes 200 times. I know I could use Excel to concatenate the strings, but I was wondering if there's a built-in command using the Spyder application. Some programs like SSMS allow you to comment or uncomment blocks of code with a single click for example.

  • 2
    try this: sql_query = ''' SELECT ID, SYSTEM_NO, SOURCE_NO, SAMP_DATETIME, STORE_NUM FROM WQI.DBO.Finding WHERE SAMP_DATE >= 20110101 ''' and then: .read_sql_query(sql_query ) – adhg Oct 04 '21 at 23:46
  • 1
    Does this answer your question? [Pythonic way to create a long multi-line string](https://stackoverflow.com/questions/10660435/pythonic-way-to-create-a-long-multi-line-string) – Charlieface Oct 04 '21 at 23:56
  • Using 3-part names is a bad habit. Your connection should determine the database to use for object references. What will happen when you change your connection string to a different database? Your queries will continue to refer to the hard-coded database, not the one in your connection. – SMor Oct 05 '21 at 02:30

0 Answers0