MCS-201 · Unit 16

Data Access Using Python

Section 5 of 7

Block 4 · Unit 16Data Access Using Python10 min · Section 5 of 7

16.5Creating Tables and Changing Data from Python

Covers IGNOU MCS-201 §16.4

Once connected, a Python program can run any SQL statement. This section creates a table and then inserts, updates and deletes rows, using the safe way of passing values and committing changes so they are saved.

Running statements that change data

  • cursor.execute(sql, params) runs one statement. Values are passed separately as a tuple, with %s placeholders in the SQL.
  • cursor.executemany(sql, list_of_tuples) runs the same statement for many rows.
  • cursor.rowcount tells how many rows the last statement affected; cursor.lastrowid gives the id generated by the last INSERT into an AUTO_INCREMENT column.
  • conn.commit() makes the changes permanent. mysql.connector does not auto-commit by default, so without commit() the changes are lost when the connection closes.

Why parameterised queries

Building SQL by joining strings with user input, such as "... WHERE name = '" + name + "'", is dangerous: a crafted input such as x' OR '1'='1 changes the meaning of the query. This attack is called SQL injection. With %s placeholders, the driver sends the values separately and they are always treated as data, never as SQL.

Key takeaways

  • execute(sql, params) with %s placeholders; executemany for batches.
  • rowcount = rows affected; lastrowid = new auto-increment id.
  • commit() saves changes; nothing is saved automatically by default.
  • Parameterised queries prevent SQL injection.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Which placeholder does mysql.connector use in parameterised queries?

Show answer

Answer: B. %s

mysql.connector uses %s for every value type; ? is used by the sqlite3 driver.

Question 2 · multiple choice

What happens to inserted rows if the program closes the connection without calling commit()?

Show answer

Answer: C. They are discarded, because autocommit is off by default

The open transaction is rolled back when the connection closes without a commit.

Question 3 · multiple choice

Which call inserts three rows in one statement call?

Show answer

Answer: C. cur.executemany(sql, [row1, row2, row3])

executemany runs the same statement once for each tuple in the list.

Question 4 · multiple choice

After cur.execute("UPDATE employee SET income = income * 1.1 WHERE sex = %s", ("F",)) on a table with two women, what is cur.rowcount?

Show answer

Answer: B. 2

rowcount reports the number of rows the statement changed: the two matching rows.

Question 5 · multiple choice

Why is cur.execute("SELECT * FROM users WHERE name = '" + name + "'") dangerous?

Show answer

Answer: D. A crafted name can change the SQL itself (SQL injection)

Input such as x' OR '1'='1 would make the condition true for every row; placeholders prevent this.

Question 6 · short answer

Write a Python fragment that inserts a new employee whose details are read from the user, safely, and prints the new employee’s id.

Show worked answer

first = input("First name: "), age = int(input("Age: ")), then cur.execute("INSERT INTO employee (first_name, age) VALUES (%s, %s)", (first, age)), conn.commit() and print("New employee id:", cur.lastrowid). The values are passed as a tuple separate from the SQL, so whatever the user types is stored as data and cannot alter the statement; commit() saves the row and lastrowid gives its auto-increment id.

↑ ↓ move↵ openTab switch type