Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- python3.6
- writes a csv to MySQL database
- """
- mydb = mysql.connector.connect(host = 'localhost',
- user = 'root',
- passwd = '****',
- db = '****')
- cursor = mydb.cursor()
- csv_file = 'path/to/file.csv'
- with open(csv_file, 'r') as file:
- csv_reader = csv.reader(file)
- next(csv_reader) #skipping the header
- for row in csv_reader:
- cursor.execute("INSERT INTO table(col1, col2, col3) VALUES (%s, %s, %s)", row)
- mydb.commit()
- cursor.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement