Guest User

Untitled

a guest
Jul 29th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. import xlrd
  2. import pymysql
  3.  
  4. #open the workbook and define rhe worksheet
  5. book = xlrd.open_workbook("d:/final/final.xls")
  6. sheet = book.sheet_by_index(0)
  7. #sheet = book.sheet_by_index(0)
  8.  
  9. #establish a mysql connection
  10. database = pymysql.connect(host="localhost", user='root', password='1234',db='test')
  11.  
  12. #get the cursor, which is used to traverse the database, line by line
  13. cursor = database.cursor()
  14.  
  15.  
  16. #create the insert into sql quety
  17. query = """INSERT INTO test2 (AriT) VALUES (%s)"""
  18.  
  19.  
  20. #create a for loop to iterate through each row in the xls file, starting at row 2 to skip the headers
  21. for r in range(1, sheet.nrows):
  22. AriT = sheet.cell(r,9).value
  23.  
  24. #assign calues from each row
  25. values = (AriT )
  26.  
  27. #execute sql query
  28. cursor.execute(query, values)
  29.  
  30. #close the cursor
  31. cursor.close()
  32.  
  33. #commit the transaction
  34. database.commit()
  35.  
  36. #close the databases connection
  37. database.close()
Add Comment
Please, Sign In to add comment