Creating Database Connections
Before wiring a database into an MCP server, you'll get some practice with creating the connection, executing parameterized queries, and closing the connections.
A database called currencies.db, containing supported currencies from the European Central Bank, is already set up and available for you to connect to.
Diese Übung ist Teil des Kurses
<Kurs>Introduction to Model Context Protocol (MCP)</Kurs>Übungsanweisungen
- Connect to the database file
"currencies.db"and assign the connection to a variable namedconn. - Execute the parameterized query, either with the
"USD"test currency code, or with a valid code of your choosing. - Close the database connection.
Interaktive praktische Übung
Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.
import sqlite3
# Connect to currencies.db
conn = sqlite3.____("currencies.db")
conn.row_factory = sqlite3.Row
# Execute the query
cursor = conn.____("SELECT code, name FROM currencies WHERE code = ? LIMIT 1", ("USD",))
row = cursor.fetchone()
print(dict(row))
# Close the connection
conn.____()