HOME/Articles/

mysql example insertDb (snippet)

Article Outline

Python mysql example 'insertDb'

Modules used in program:

  • import mysql.connector

python insertDb

Python mysql example: insertDb

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="",
  database="python_tutorial"
)
mycursor = mydb.cursor()

sql = "INSERT INTO user (name, age, sex) VALUES (%s, %s, %s)"
val = ("John", 15, "M")
mycursor.execute(sql, val)

mydb.commit()

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