Skip to content

Language

Sign in

Learn › Class 12 › Computer Science / Informatics Practices › Python and MySQL connectivity

Python and MySQL connectivity

Example 1: Fetch students above a cut-off

Open one step at a time. Try the next step yourself first.

  1. Step 1. import mysql.connector as m
  2. Show step 2

    con = m.connect(host='localhost', user='root', password='***', database='school')

  3. Show step 3

    cur = con.cursor()

  4. Show step 4

    cur.execute('SELECT Name FROM STUDENT WHERE Marks > %s', (80,))

  5. Show step 5

    for row in cur.fetchall(): print(row[0])

  6. Show step 6

    con.close()

Example 2: Insert a row and save it

Open one step at a time. Try the next step yourself first.

  1. Step 1. cur.execute('INSERT INTO STUDENT VALUES (%s, %s, %s, %s)', (21, 'Kabir', 12, 88))
  2. Show step 2

    con.commit() makes the change permanent.

  3. Show step 3

    cur.rowcount is 1, because one row was inserted.

Reviewed by EduFlint team on 1 Dec 2026 · version 1

Written by us for this site. Independent; not affiliated with NCERT or CBSE.

Report a mistake

Looking for another topic?