I have connected to a SQL database through python and am trying to extract one column of data. However, when I go to print what I extracted, it displays the following:
('A', )
('B', )
('C', )
('D', )
When I extract two columns, it prints:
('A', 'a' )
('B', 'b')
('C', 'c')
('D', 'd')
Is there a way to simply take the data in the first column without the additional , ) or even transform into a dataframe while preserving the column names from SQL? I have found a lot of guides how to extract data from SQL, but none showing ability to operate as a dataframe and preserve column names.
Code to generate:
import pyodbc
conn = pyodbc.connect(driver='{SQL Server}'
,server = 'server'
,database = 'db1'
,trusted_connection=True
,user='user')
cursor=conn.cursor()
cursor.execute(
'''SELECT
Column_One
--,Column_Two
FROM db1.table''')
for row in cursor.fetchall():
print(row)