mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
1.7 KiB
1.7 KiB
SQL Interpolation
SQL Interpolation, available in Swift 5, lets you embed values in your SQL queries by wrapping them inside \( and ):
let name: String = ...
let id: Int64 = ...
try db.execute(literal: "UPDATE player SET name = \(name) WHERE id = \(id)")
SQL interpolation looks and feel just like regular Swift interpolation:
let name = "World"
print("Hello \(name)!") // prints "Hello World!"
The difference is that it generates valid SQL which does not suffer from syntax errors or SQL injection. For example, you do not need to validate input or process single quotes:
// Correctly executes UPDATE player SET name = 'O''Brien' WHERE id = 42
let name = "O'Brien"
let id = 42
try db.execute(literal: "UPDATE player SET name = \(name) WHERE id = \(id)")
Under the hood, SQL interpolation generates a plain SQL query string, as well as statement arguments. It runs exactly as below:
try db.execute(sql: "UPDATE player SET name = ? WHERE id = ?", arguments: [name, id])
SQL interpolation is handy and safe, but you may also want to build your own raw SQL strings:
-
👉 For raw SQL strings, use the
sqlargument label:// sql: Raw SQL string try db.execute(sql: "UPDATE player SET name = ? WHERE id = ?", arguments: [name, id]) -
👉 For SQL interpolation, use the
literalargument label:// literal: SQL Interpolation try db.execute(literal: "UPDATE player SET name = \(name) WHERE id = \(id)")