Reputation: 41
I have query in .txt file and i am trying to run that query using python. it is working fine if my query written in single line. but my query had multiple lines in text file. it is giving syntax error as it is reading only first line.
i have tried below code
cursor = cnxn.cursor()
with open('C:\Python_Script_Test\INSERTS.txt','r') as inserts:
for statement in inserts:
cursor.execute(statement)
i have big query with multiple lines in it. can you please suggest the best code to read all the lines to run query.
Upvotes: 1
Views: 10923
Reputation: 1
.read() works for single line query. For multiline query Python creates list of lines. You can collate lines together using .append(), but you will need to add CRLF markers at the end of each line that are readable by SQL server ...
Upvotes: 0
Reputation: 82765
Try using .read()
Ex:
cursor = cnxn.cursor()
with open('C:\Python_Script_Test\INSERTS.txt','r') as inserts:
query = inserts.read()
cursor.execute(query)
Upvotes: 2