Python Database Handling

Python Database Handling

Last Updated :
Discuss
Comments

Question 1

Which Python library is commonly used to connect with MySQL databases?

  • sqlite3

  • pymysql

  • psycopg2

  • sqlalchemy

Question 2

What does the following code do?

Python
conn = pymysql.connect(
    host="localhost",
    user="root",
    password="password",
    db="testdb"
)
  • Connects to a MongoDB server

  • Establishes a MySQL connection (correct)

  • Creates a new MySQL database

  • Imports a SQL dump

Question 3

What does rows contain after executing this code?

Python
cursor = conn.cursor()
cursor.execute("SELECT * FROM users")
rows = cursor.fetchall()



  • A dictionary of column names

  • A list of column headers

  • A list of tuples representing each row (correct)

  • A single row as a tuple

Question 4

Which of the following is not required when establishing a MySQL connection using pymysql?

  • Hostname

  • Port

  • Database name

  • Collection name

Question 5

What is the purpose of calling conn.commit() after an INSERT query in MySQL?

  • To ensure changes are saved permanently

  • To roll back the transaction

  • To terminate the connection

  • To check if the connection is active

Question 6

What will be the result of executing this query?


cursor.execute("SELECT * FROM employees WHERE salary > %s", (50000,))

  • SQL syntax error due to placeholder

  • Raises a type error

  • Executes safely using parameter substitution

  • Always returns an empty result

Question 7

Which library allows Python to interact with MongoDB?

  • pymysql

  • mysql.connector

  • pymongo

  • sqlalchemy

Question 8

What does the following code assign to db?


client = pymongo.MongoClient("mongodb://localhost:27017/")

db = client["mydb"]


  • A table in the database

  • A MongoDB document

  • A collection

  • A reference to the mydb database

Question 9

What does this command do in MongoDB?


db.users.insert_one({"name": "Alice", "age": 25})

  • Adds a record to a MySQL table

  • Adds a document to MongoDB

  • Creates a new database

  • Deletes an existing document

Question 10

Which of the following statements is true about MongoDB’s schema design?

  • It must be predefined

  • All documents must have the same fields

  • Schema is dynamic and flexible

  • MongoDB doesn't use schemas at all

There are 12 questions to complete.

Take a part in the ongoing discussion