Interface of Python with an SQL Database - CBSE Class 12 Computer Science Notes, Diagrams and Solved Questions

Connect a Python program to MySQL with mysql.connector, run queries through a cursor, read rows with fetchone and fetchall, save changes with commit, and pass values safely with %s placeholders.

In this chapter

  • Installing and importing mysql.connector
  • connect() and checking the connection
  • Cursors and execute()
  • fetchone(), fetchall() and rowcount
  • INSERT, UPDATE and DELETE from Python, and commit()
  • Building queries with %s placeholders and format()
  • Why parameterised queries are safer than joining strings