Learn › Class 12 › Computer Science / Informatics Practices › Python and MySQL connectivity
Python and MySQL connectivity
Work it out on paper first. Then check the hint or the answer.
1. Which connection method makes INSERT, UPDATE and DELETE changes permanent?
Hint
It is called on the connection object.
Answer and explanation
Answer: commit()
con.commit() saves the changes of the current transaction.
2. A table has 10 rows. After cur.execute('SELECT * FROM T') and cur.fetchmany(3), how many rows does cur.fetchall() return?
Hint
fetchall() returns only the remaining rows.
Answer and explanation
Answer: 7
3 rows were already fetched, so 10 − 3 = 7 remain.
3. What does fetchone() return when there are no more rows?
Hint
It is Python's 'no value' object.
Answer and explanation
Answer: None
fetchone() returns None when the result set is used up.
4. Why should you use %s placeholders instead of joining strings into a query?
Hint
Think about what a user could type.
Answer and explanation
Answer: To prevent SQL injection
Placeholders send the values separately, so input can never change the query's structure.