Python Forum
Reading SQL scripts from excel file and run it using python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading SQL scripts from excel file and run it using python
#1
Hi All,

I placed my SQL scripts in excel file and my python code needs to read the SQL code from excel and then run the SQL code from SQL Server using Python . If the fetch row is greater than zero then it is 'Failed' else 'Passed'. This failed or Passed results needs to be writes into Excel file as output.
What I tried do far . I am able to run the SQL scripts directly
import pyodbc 
conn = pyodbc.connect('Driver={SQL Server};'
                      'Server=ServerName;'
                      'Database=DatabaseName;'
                      'Trusted_Connection=yes;')

cursor = conn.cursor()
cursor.execute('SELECT * FROM  [DUMMY]')
title = [i[0] for i in cursor.description]
print (title)
for row in cursor:
    print(row)
I am able to read the excel files separately . When I merge both the code I am getting error from SQL code part

import xlrd
file_location= "pathname"
workbook =xlrd.open_workbook(file_location)
sheet =workbook.sheet_by_index(0)
sheet.cell_value(0,0)
Reply
#2
Tell us what error you get and how you merged both codes. Perhaps we can then help you.
Reply
#3
Thanks Ibreedan

Now python code is working fine. But 2 problem's
1) I am not getting column header name when I run SQL scripts through Python.
2) How to auto increment next row in excel file. E.g If my Cell value is (0,0) and if I want to read the SQL script available in Cell value (1,0) . How to do it ?


import xlrd
import pyodbc 
conn = pyodbc.connect('Driver={SQL Server};'
                      'Server=Servername'
                      'Database=PythonSQL;'
                      'Trusted_Connection=yes;')
file_location= "E:/Python Automation/file.xls"
workbook =xlrd.open_workbook(file_location)
sheet =workbook.sheet_by_index(0)
query=sheet.cell_value(0,0)
cursor = conn.cursor()
cursor.execute(query)
for row in cursor:
    print(row)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python openyxl not updating Excel file MrBean12 1 306 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 406 Feb-07-2024, 12:24 PM
Last Post: Viento
Sad problems with reading csv file. MassiJames 3 608 Nov-16-2023, 03:41 PM
Last Post: snippsat
  Search Excel File with a list of values huzzug 4 1,211 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 806 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  trouble reading string/module from excel as a list popular_dog 0 413 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  Reading a file name fron a folder on my desktop Fiona 4 889 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  Trying to us python.exe from our network to run scripts cubangt 3 846 Aug-17-2023, 07:53 PM
Last Post: deanhystad
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,083 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Save and Close Excel File avd88 0 2,972 Feb-20-2023, 07:19 PM
Last Post: avd88

Forum Jump:

User Panel Messages

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