在表格中插入记录

  1. import mysql.connector
  2.  
  3. mydb = mysql.connector.connect(
  4. host="localhost",
  5. user="myusername",
  6. passwd="mypassword",
  7. database="mydatabase"
  8. )
  9.  
  10. mycursor = mydb.cursor()
  11.  
  12. sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
  13. val = ("John", "Highway 21")
  14.  
  15. mycursor.execute(sql, val)
  16.  
  17. mydb.commit()
  18.  
  19. print(mycursor.rowcount, "record inserted.")