Siddharth C
Siddharth C

Reputation: 39

Error while retrieving data from SQL Server using pyodbc python

My table data has 5 columns and 5288 rows. I am trying to read that data into a CSV file adding column names. The code for that looks like this :

cursor = conn.cursor()
cursor.execute('Select * FROM classic.dbo.sample3')

rows = cursor.fetchall()

print ("The data has been fetched")

dataframe = pd.DataFrame(rows, columns =['s_name', 't_tid','b_id', 'name', 'summary'])
dataframe.to_csv('data.csv', index = None)

The data looks like this

s_sname   t_tid   b_id  name   summary
---------------------------------------------------------------------------
db1       001     100   careie  hello this is john speaking blah blah blah

It looks like above but has 5288 such rows.

When I try to execute my code mentioned above it throws an error saying :

ValueError: Shape of passed values is (5288, 1), indices imply (5288, 5)

I do not understand what wrong I am doing.

Upvotes: 0

Views: 96

Answers (1)

DeepBlue
DeepBlue

Reputation: 448

Use this.

dataframe = pd.read_sql('Select * FROM classic.dbo.sample3',con=conn)
dataframe.to_csv('data.csv', index = None)

Upvotes: 1

Related Questions