BaşlayınÜcretsiz Başlayın

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.

Bu egzersiz

Building Chatbots in Python

kursunun bir parçasıdır
Kursu Görüntüle

Egzersiz talimatları

  • 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.

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

# 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(____)
Kodu Düzenle ve Çalıştır