Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try:
- connection=mysql.connector.connect(user='root', password ='12345', database='sakila')
- cursor=connection.cursor()
- sql=("select picture from staff")
- cursor.execute(sql)
- result=cursor.fetchall()
- variables = cursor.column_names
- with open("MySQL-connector_csv.csv", "w", newline='n', encoding='UTF-8') as c:
- writer = csv.writer(c)
- writer.writerow(variables)
- for data in result:
- writer.writerow(data)
- connection.commit()
- sql=("CREATE TABLE IF NOT EXISTS test (ID INTEGER AUTO_INCREMENT,IMAGE BLOB,PRIMARY KEY (ID));")
- cursor.execute(sql)
- connection.commit()
- with open("MySQL-connector_csv.csv", "r", encoding="UTF-8") as file:
- csv_data = csv.reader(file, delimiter =',')
- next(csv_data, None) #skip the variable names
- for row in csv_data:
- sql = "INSERT INTO `test` (`IMAGE`) VALUES (%s)"
- cursor.execute(sql, row)
- connection.commit()
- finally:
- connection.close()
Add Comment
Please, Sign In to add comment