SQL-Abfragen sicher bereitstellen
Bieten Sie Lesezugriff auf eine Datenbank ohne SQL-Injection.
SQL-Abfragen sicher bereitstellen ist eine kostenlose MCP Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des MCP Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der MCP Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Databases Behind a Tool
An MCP server can give a model real data by wrapping a database behind a tool, so the AI asks questions instead of touching raw tables. 🗄️
Reads, Not Writes
For safety, start by exposing only read access. Let the model query and report, but keep inserts, updates, and deletes off the table.
The Injection Danger
Never paste model text straight into SQL. That invites SQL injection, where crafted input rewrites your query and leaks or destroys data.
query = "SELECT * FROM users WHERE id = " + user_input # unsafe!Use Parameterized Queries
The fix is parameterized queries: you write placeholders and the driver binds values safely, so input can never change the query shape.
cur.execute("SELECT * FROM users WHERE id = ?", (user_id,))Take Values as Arguments
Let the model pass values as typed arguments, then you build the query. The model supplies data, never the SQL text itself.
@mcp.tool()
def find_user(user_id: int) -> str:
...Pin the Query Shape
Keep the SQL fixed in your code and only slot in bound parameters. A locked query shape is the simplest way to stay safe.
Use a Read-Only Role
Defense in depth: connect with a database read-only user. Even a buggy tool then physically cannot modify or drop your data.
Bound the Result Size
Always cap rows with a LIMIT so one broad query cannot dump a huge table into the model and blow your token budget.
cur.execute("SELECT name FROM users LIMIT 50")Whitelist Allowed Tables
If the model picks a table, validate it against an allowlist. Reject anything not on the list instead of trusting the name.
Return Tidy Rows
Format results as clean text or simple records so the model reads them easily. Clarity in, clarity out for the next reasoning step.
Hide Sensitive Columns
Select only the columns the task needs. Leaving out secrets like password hashes keeps private data out of the model entirely.
Quick Check
What protects a SQL tool from injection?
Recap: Safe SQL
You exposed a database safely: parameterized read-only queries, allowlisted tables, and row limits. Next, wrap a web API. 🎯
Häufig gestellte Fragen
Ist die Lektion „SQL-Abfragen sicher bereitstellen“ kostenlos?
Ja — der vollständige Text von „SQL-Abfragen sicher bereitstellen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des MCP Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der MCP Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „SQL-Abfragen sicher bereitstellen“?
Bieten Sie Lesezugriff auf eine Datenbank ohne SQL-Injection. Du übst MCP Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um MCP Academy zu starten?
Keine Vorkenntnisse erforderlich. MCP Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.
Wie lange dauert die Lektion „SQL-Abfragen sicher bereitstellen“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser MCP Academy-Lektion Code schreiben und ausführen?
Ja. Jede MCP Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- SQL-Abfragen sicher bereitstellen
- Eine REST-API als Tools verpacken
- Verbindungen während der Lebensdauer bündeln
- Upstreams zwischenspeichern und begrenzen