I am trying to build an desktop app to generate sql queries from excel through pandas data frame. i am able to generate insert statement , but i am receiving data in time_stamp format, i want convert it into to_date format,please suggest a better way to do this. also please suggest to generate select statement by making use of same code
here is my code:
from pandas import *
table_name="ADI"
file_name=pandas.read_excel('supermarke.xlsx')
def SQL_Insert(SOURCE, TARGET):
sql_texts = []
for index, row in SOURCE.iterrows():
sql_texts.append(
'INSERT INTO ' + TARGET + ' (' + str(', '.join(SOURCE.columns)) + ') VALUES ' + str(tuple(row.values))+";")
return ('\n'.join(sql_texts))
print(SQL_Insert(file_name, table_name))
here is my result:-
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (1, '3666 21st St', 'San Francisco', 'CA 94114', 'USA', 'Madeira', 8, Timestamp('2018-01-12 00:00:00'));
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (2, '735 Dolores St', 'San Francisco', 'CA 94119', 'USA', 'Bready Shop', 15, Timestamp('2018-01-12 00:00:00'));
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (3, '332 Hill St', 'San Francisco', 'California 94114', 'USA', 'Super River', 25, Timestamp('2018-01-12 00:00:00'));
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (4, '3995 23rd St', 'San Francisco', 'CA 94114', 'USA', "Ben's Shop", 10, Timestamp('2018-01-12 00:00:00'));
and i am trying to add one more functionality , if file not found shows error message.
excel file
@Chirag, if i have empty cell value i am receiving output like nan, but when i am going to insert this i am not able to insert it because sql use null, instead of nan, any help in this.?
INSERT INTO ADI (PLAN_ID, DEVICE_ID, PLAN_CONTRACT_DURATION, DEVICE_CONTRACT_DURATION, SALES_CHANNEL, VENDOR_TYPE, EFFECTIVE_DATE, EXPIRATION_DATE, PLAN_NAME, DEVICE, RRP, DEVICE_REPAYMENT, TOTAL_REPAYMENT_CHARGES, TOTAL_CREDIT_CHARGES, URL, EX_VENDOR_TYPE) VALUES (20637411, 20663271, 1, 1, 'ALL', 'ALL', Timestamp('2018-10-30 00:00:00'), Timestamp('2050-12-31 00:00:00'), 'Unlimited data Home Wireless ($79 Vividwireless)', 'Huawei B315 ', 0, 0, 199, 0, nan, nan);
how to replace nan with NULL/null ?

code-blocknot via links or image.