Questions
user482user482asked 4 months ago
I am a beginner at phyton, and I want to know how to write the MySQL INSERT statement in Python.
1

1 Answers

Accepted
codegenlabcodegenlabanswered 4 months ago
First install MySQL and MySQL connector, if you don’t know to install check this link, then try this code,

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword",
  database="mydatabase"
)

mycursor = mydb.cursor()

sql = "INSERT INTO customers (name, gender) VALUES (%s, %s)"
val = ("John", "male")
mycursor.execute(sql, val)

mydb.commit()

print(mycursor.rowcount, "record inserted.")
1