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.
- Step 1. import mysql.connector as m
Show step 2
con = m.connect(host='localhost', user='root', password='***', database='school')
Show step 3
cur = con.cursor()
Show step 4
cur.execute('SELECT Name FROM STUDENT WHERE Marks > %s', (80,))
Show step 5
for row in cur.fetchall(): print(row[0])
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.
- Step 1. cur.execute('INSERT INTO STUDENT VALUES (%s, %s, %s, %s)', (21, 'Kabir', 12, 88))
Show step 2
con.commit() makes the change permanent.
Show step 3
cur.rowcount is 1, because one row was inserted.