Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import xlrd
- import pymysql
- #open the workbook and define rhe worksheet
- book = xlrd.open_workbook("d:/final/final.xls")
- sheet = book.sheet_by_index(0)
- #sheet = book.sheet_by_index(0)
- #establish a mysql connection
- database = pymysql.connect(host="localhost", user='root', password='1234',db='test')
- #get the cursor, which is used to traverse the database, line by line
- cursor = database.cursor()
- #create the insert into sql quety
- query = """INSERT INTO test2 (AriT) VALUES (%s)"""
- #create a for loop to iterate through each row in the xls file, starting at row 2 to skip the headers
- for r in range(1, sheet.nrows):
- AriT = sheet.cell(r,9).value
- #assign calues from each row
- values = (AriT )
- #execute sql query
- cursor.execute(query, values)
- #close the cursor
- cursor.close()
- #commit the transaction
- database.commit()
- #close the databases connection
- database.close()
Add Comment
Please, Sign In to add comment