Python Interview Questions on Python Database Interface

We have compiled most frequently asked Python Interview Questions which will help you with different expertise levels.

Python Interview Questions on Python Database Interface

Question 1:
What is the generic Python “built-in” database module called?
Answer:
DBM

Question 2:
What are some advantages of using DBM?
Answer:
No back-end database is required. The files are portable and work with the pickle and shelve modules to store Python objects.

Question 3:
What does it mean to pickle and unpickle a Python object?
Answer:
Using the pickle module, allows the writing of Python objects to a file. Unpickling is the reverse, reading a file containing a pickled Python object and re-instantiating it in the running program.

Question 4:
What is the difference between the pickle and cPickle Python modules?
Answer:
cPickle is much faster, but it cannot be subclassed like pickle can.

Question 5:
What is the shelve module, and how is it different from pickle?
Answer:
Shelving an object is a higher-level operation that pickles, in that the shelve module provides its own open method and pickles objects behind the scene. Shelve also allows the storage of more complex Python objects.

Question 6:
Illustrate creating a DBM file.
Answer:
import anydbm
myDB = anydbm.open(“newDBFile.dbm”, ‘n’)

Question 7:
Illustrate writing a list to a DBM file.
Answer:
import anydbm
my List = [“This”, “That”, “Something Else”]
myDB = anydbm.open(“newDBFile.dbm”, ‘w’)
mylndex = 0
for myltem in myList:
myDB[my!tem] = myList[myIndex]
mylndex += 1 myDB.close( )

Question 8:
Illustrate pickling a list.
Answer:
import cPickle .
myList = [“This”, “That”, “Something Else”]
myFile = open(“myPickleFile.dat”, “w”)
myPickler = cPickle.Pickler(myFile)
myPickler.dump(myList)
myFile.close( )

Question 9:
Illustrate shelving a dictionary.
Answer:
import shelve
myDict = {“name”: “John Doe”, “Address” : “1234 Main street”}
my Shelf = shelve.open(” my ShelveFile.dat”, “n”)
my Shelfl” Dictionary”] = myDict
my Shelf. close( )

Question 10:
Illustrate retrieving a dictionary from a shelf file Answer:
import shelve
myShelf= shelve.open(” my ShelveFile.dat”, “r”)
myDict = my Shelfl “Dictionary”]
myShelf.close( )

Question 11:
What Python module is used to work with a MySQL database?
Answer:
MySQLdb.

Question 12:
Illustrate connecting to a MySQL database on the local host, with a username and a password.
Answer:
import MySQLdb
myDB = MySQLdb.connect(host=”127.0.0.1″, user=”username”, passwd=”password”)

Question 13:
What is a MySQLdb cursor?
Answer:
It is a handle that lets you send SQL commands to MySQL and retrieve the result.

Question 14:
Illustrate creating a cursor, then sending a select
command to MySQL
Answer:
import MySQLdb
myDB = MySQLdb.connect(host=” 127.0.0.1″, usem”username”,
passwd= “password “)
my Cursor = myDB.cursorO
myCursor.execute(” select * from tablename”)
myResults = myCursor.fetchall( )

Question 15:
How are databases and tables created in MySQL from Python?
Answer:
Using a database connection and cursor to execute the appropriate CREATE DATABASE and CREATE TABLE SQL commands.

Question 6:
How is the currently selected database name retrieved?
Answer:
Using a database connection and cursor to execute a SELECT DATABASE() SQL command.

Question 17:
What is the .fetchall( ) method of the cursor object?
Answer:
It is used to retrieve all of the results of the executed SQL command. It returns a series of one or more lists depending on the results available.

Question 18:
Illustrate adding a row to a MySQL database
Answer:
import MySQLdb
myDB = MySQLdb.connect(host=”127.0.0.1”, user=”username”, passwd= “password “)
myCursor = myDB.cursor( )
my Cursor. execute(” INSERT INTO tablename VALUES ‘Vail’, ‘Val2’, ‘etc.'”)
myDB. commit( )

Question 19:
What does the .commit method of the database object do?
Answer:
It flushes the pending request buffer and ensures that the transactions are all written to disk.

Question 20:
Illustrate retrieving a list of available tables in a MySQL database.
Answer: „
import MySQLdb
myDB = MySQLdb.connect(host=” 127.0.0.1″, user=”username”, passwd= “password “)
myCursor = myDB.cursor ( )
myCursor.executeC’SHOW TABLES “)
myResults = myCursor.fetchall( )