Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  1. """
  2. python3.6
  3. writes a csv to MySQL database
  4. """
  5.  
  6. mydb = mysql.connector.connect(host = 'localhost',
  7. user = 'root',
  8. passwd = '****',
  9. db = '****')
  10. cursor = mydb.cursor()
  11. csv_file = 'path/to/file.csv'
  12. with open(csv_file, 'r') as file:
  13. csv_reader = csv.reader(file)
  14. next(csv_reader) #skipping the header
  15. for row in csv_reader:
  16. cursor.execute("INSERT INTO table(col1, col2, col3) VALUES (%s, %s, %s)", row)
  17. mydb.commit()
  18. cursor.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement