๐Ÿ”ฅ 75% OFF Early-bird โ‚น2,999 one time โณ BOARDS ARE CLOSE Lock this price before it goes up. Grab my seat โ†’
Subjects โ€บ Computer Science โ€บ Python-MySQL Connectivity

Python-MySQL Connectivity

๐Ÿ”’ Priority & marks: full access ยท 12 questions shown
๐Ÿ“Œ How to approach this chapter

In a typical paper this chapter carries about 4.1 marks. Fixed code template - learn once.

  • What the questions look like: a 2-5 mark program that connects to MySQL and inserts, updates, deletes, or displays rows, or a 1-mark MCQ on cursor / fetch / commit. The exam gives username, password, host and database name.
  • Template: import mysql.connector as sq; con = sq.connect(host="localhost", user="admin", passwd="root", database="SCHOOL"); cur = con.cursor(); cur.execute("SELECT ..."); rows = cur.fetchall(); for r in rows: print(r); con.close().
  • fetchone() returns one row, fetchmany(n) n rows, fetchall() all remaining rows (as a list of tuples); cur.rowcount gives the number of rows affected/fetched. INSERT, UPDATE and DELETE need con.commit() or the change is not saved.
  • Putting values in a query: "INSERT INTO t VALUES (%s, %s)" with cur.execute(query, (a, b)), or the format() / f-string way - either is accepted. Check the quotes around string values.
You can try the first few questions here. Start free for 5 days to open everything.