I am working on a script to load information from excel to SQL. I cannot figure out how to fix my loops to remove the extra comma and extra space I get at the end of my sql code..
This is python code meant to read an excel file, and create INSERT INTO querys...
This is a python script meant to pull data from an excel file
import xlrd
with open('Phase3initial.sql', 'w') as file:
data = xlrd.open_workbook('Phase3 Initial Data.xlsx', on_demand = True)
sheets = data.sheet_names()
tables = ['User', 'UserEmail', 'Employee', 'Site', 'Event', 'Transit', 'Connect', 'TakeTransit', 'AssignTo', 'VisitEvent', 'VisitSite']
for i in range(data.nsheets):
sheets[i] = data.sheet_by_index(i)
for j in range (1, sheets[i].nrows):
file.write('INSERT INTO ' + str(tables[i]) + '(')
row = sheets[i].row_values(j)
for k in range(len(row)):
cell = sheets[i].cell(j,k)
if tables[i] == 'User':
if k ==1:
file.write(str(cell.value) +', ')
continue
file.write(str(cell.value) + ', ')
file.write(');\n')
image of excel file https://imagizer.imageshack.com/v2/1024x768q90/924/EOTu41.png
I get an extra space and comma at the end.. after my code runs..Not sure how to fix that..
INSERT INTO User(james.smith, jsmith123, Approved, James, Smith, Employee, );
INSERT INTO User(michael.smith, msmith456, Approved, Michael, Smith, Employee, Visitor, );
INSERT INTO UserEmail(james.smith, jsmith@gatech.edu, );
INSERT INTO UserEmail(james.smith, jsmith@outlook.com, );
Edit After some trial an error I am still stuck with the Date format.. Also, any ideas on how to hide the password? It's not supposed to be visible..
import xlrd
with open('Phase3initial.sql', 'w') as file:
data = xlrd.open_workbook('Phase3 Initial Data.xlsx', on_demand = True)
sheets = data.sheet_names()
tables = ['User', 'UserEmail', 'Employee', 'Site', 'Event', 'Transit', 'Connect', 'TakeTransit', 'AssignTo', 'VisitEvent', 'VisitSite']
for i in range(data.nsheets):
sheets[i] = data.sheet_by_index(i)
for j in range (1, sheets[i].nrows):
file.write('INSERT INTO ' + str(tables[i]) + '(')
row = sheets[i].row_values(j)
# first len(row)-1 columns
for k in range(len(row)-1):
cell = sheets[i].cell(j,k)
if tables[i] == 'User':
if k ==1:
file.write(str(cell.value) +',')
continue
file.write(str(cell.value) + ',')
# last column
k = len(row) - 1
cell = sheets[i].cell(j, k)
if tables[i] == 'User':
if k == 1:
file.write(str(cell.value))
continue
file.write(str(cell.value))
file.write(');\n')
For some reason my date is changed to 435000.0... is not formated.. check StartDate and EndDate on link https://imagizer.imageshack.com/v2/1024x768q90/921/uODMTH.png after james.smith and michael.smith the password are showing.. any ideas on how to hide the passwords?
My output
INSERT INTO User(james.smith,jsmith123,Approved,James,Smith,Employee);
INSERT INTO User(michael.smith,msmith456,Approved,Michael,Smith,Employee, Visitor);
INSERT INTO Event(Eastside Trail,43500.0,Piedmont Park,43501.0,0.0,99999.0,1.0,A combination of multi-use trail and linear greenspace, the Eastside Trail was the first finished section of the Atlanta BeltLine trail in the old rail corridor. The Eastside Trail, which was funded by a combination of public and private philanthropic sources, runs from the tip of Piedmont Park to Reynoldstown. More details at https://beltline.org/explore-atlanta-beltline-trails/eastside-trail/,);
INSERT INTO Event(Eastside Trail,43500.0,Inman Park,43501.0,0.0,99999.0,1.0,A combination of multi-use trail and linear greenspace, the Eastside Trail was the first finished section of the Atlanta BeltLine trail in the old rail corridor. The Eastside Trail, which was funded by a combination of public and private philanthropic sources, runs from the tip of Piedmont Park to Reynoldstown. More details at https://beltline.org/explore-atlanta-beltline-trails/eastside-trail/,);