How do you connect to a SQL database in python? (AI) Flashcards

(20 cards)

1
Q

What Python standard governs database connections?

A

The Python Database API Specification (DBAPI), usually referred to as PEP 249.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What Python module is used for connecting to SQLite databases?

A

The built-in sqlite3 module.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the first object you must create to interact with a database?

A

The Connection object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Example: Establishing a connection to a SQLite file named ‘app.db’

A

conn = sqlite3.connect('app.db')

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What object do you create from the connection to execute SQL queries?

A

The Cursor object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Example: Creating a cursor object

A

cursor = conn.cursor()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you execute an SQL command (like CREATE TABLE)?

A

Use the cursor’s execute() method: cursor.execute("CREATE TABLE users (id, name)")

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you ensure changes (like INSERT or UPDATE) are permanently saved to the database?

A

Call the commit() method on the connection object: conn.commit()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you run a SELECT query and retrieve one result row?

A

Use the fetchone() method on the cursor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you run a SELECT query and retrieve all result rows?

A

Use the fetchall() method on the cursor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you safely close the connection after you are done?

A

Call the close() method on both the cursor and the connection: cursor.close() and conn.close()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What keyword is often used to ensure connection and cursor objects are closed automatically?

A

The with keyword (used for context management).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Example: Connection using the with statement

A

with sqlite3.connect('app.db') as conn:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What common third-party library is often used for connecting to PostgreSQL?

A

psycopg2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What common third-party library is often used for connecting to MySQL?

A

mysql-connector-python or PyMySQL

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Why should you never use f-strings to insert data into an SQL query?

A

It exposes the application to SQL Injection attacks.

17
Q

How should you pass variable data into an SQL query safely?

A

Use question marks (?) as placeholders in the query string and pass the variables as a tuple to the execute() method.

18
Q

Example: Safe insertion of a name variable

A

name = 'Alice'\ncursor.execute("INSERT INTO users VALUES (?, 'password')", (name,))

19
Q

What is an ORM (Object-Relational Mapper)?

A

A library (like SQLAlchemy) that allows you to interact with your database using object-oriented code instead of raw SQL.

20
Q

What happens if you forget to call conn.commit() after an INSERT statement?

A

The change will not be saved to the database