INSERT — Python Technical Interview Questions & Answers
INSERT
INSERT
- INSERT is about talking to a database from Python. Pattern is always: connect → cursor → execute → fetch or commit → close.
- For INSERT: use ? placeholders (or the driver’s params). Never build SQL with + user text — that is injection. sqlite3 is in the stdlib for practice; MySQL/Mongo need a driver.
- INSERT on the board: insert Asha, select her back, print the name. Same idea on MySQL. Close the connection.
- Output: Asha. Parameterized insert — never + user text into SQL.
Technical Interview Tip: Steps in order: connect → execute → fetch/commit → close. Mention placeholders.
Code Examples for INSERT
Example
# INSERT
import sqlite3
con = sqlite3.connect(":memory:")
cur = con.cursor()
cur.execute("create table t(name text)")
cur.execute("insert into t values(?)", ("Asha",))
con.commit()
print(cur.execute("select name from t").fetchone()[0])
con.close()
Output: Asha. Parameterized insert — never + user text into SQL.
How to Explain INSERT to an Interviewer
Explain INSERT as if you are teaching a junior — definition, then one tiny script.
INSERT uses connect + cursor + SQL/API. INSERT — parameterized queries. Never + user text into SQL.
What does the example print, and why?
Asha
What mistake do freshers make with INSERT?
INSERT — commit after write. close when done.