Guest User

Untitled

a guest
Jun 11th, 2018
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. try:
  2. connection=mysql.connector.connect(user='root', password ='12345', database='sakila')
  3. cursor=connection.cursor()
  4. sql=("select picture from staff")
  5. cursor.execute(sql)
  6. result=cursor.fetchall()
  7. variables = cursor.column_names
  8.  
  9. with open("MySQL-connector_csv.csv", "w", newline='n', encoding='UTF-8') as c:
  10. writer = csv.writer(c)
  11. writer.writerow(variables)
  12. for data in result:
  13. writer.writerow(data)
  14. connection.commit()
  15.  
  16.  
  17. sql=("CREATE TABLE IF NOT EXISTS test (ID INTEGER AUTO_INCREMENT,IMAGE BLOB,PRIMARY KEY (ID));")
  18. cursor.execute(sql)
  19. connection.commit()
  20.  
  21. with open("MySQL-connector_csv.csv", "r", encoding="UTF-8") as file:
  22. csv_data = csv.reader(file, delimiter =',')
  23. next(csv_data, None) #skip the variable names
  24. for row in csv_data:
  25. sql = "INSERT INTO `test` (`IMAGE`) VALUES (%s)"
  26. cursor.execute(sql, row)
  27. connection.commit()
  28.  
  29.  
  30. finally:
  31. connection.close()
Add Comment
Please, Sign In to add comment