LoslegenKostenlos loslegen

SQL statements in Python

It's time to begin writing SQL queries! In this exercise, your job is to run a query against the hotels database to find all the expensive hotels in the south. The connection to the database has been created for you, along with a cursor c.

As Alan described in the video, you should be careful about SQL injection. Here, you'll pass parameters the safe way: As an extra tuple argument to the .execute() method. This ensures malicious code can't be injected into your query.

Diese Übung ist Teil des Kurses

Building Chatbots in Python

Kurs anzeigen

Anleitung zur Übung

  • Define a tuple t of strings "south" and "hi" for the area and price.
  • Execute the query using the cursor's .execute() method. You're looking for all of the fields for all hotels where the area is "south" and the price is "hi".
  • Print the results using the cursor's .fetchall() method.

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

# Import sqlite3
import sqlite3

# Open connection to DB
conn = sqlite3.connect('hotels.db')

# Create a cursor
c = conn.cursor()

# Define area and price
area, price = "____", "____"
t = (____, ____)

# Execute the query
c.____('SELECT ____ FROM ____ WHERE ____=____ AND ____=____', ____)

# Print the results
print(____)
Code bearbeiten und ausführen