Skip to content

Structured API for SQLite CREATE [TEMP] VIEW AS #1394

Answered by groue
mallman asked this question in Q&A
Discussion options

You must be logged in to vote

Hello @mallman, there's no CREATE VIEW api yet, you're right :-)

SQLite does not accept arguments ? in statements that modify the schema, as you noticed1.

GRDB has internal apis that expand arguments into SQL literals. For example:

// NOT PUBLICLY EXPOSED
let value = "O'Brien"
let literal: SQL = "WHERE name = \(value)"

try dbPool.write { db in
    let context = SQLGenerationContext(db, argumentsSink: .forRawSQL)
    // "WHERE name = 'O''Brien'"
    let sql = try sqlLiteral.sql(context)
}

That's how the table creation apis can accept arguments:

// Runs `CREATE TABLE player(name TEXT DEFAULT 'O''Brien')`
// Instead of `CREATE TABLE player(name TEXT DEFAULT ?)`
let defaultName = "O'Brien"
try

Replies: 3 comments 6 replies

Comment options

mallman
Jun 15, 2023
Collaborator Author

You must be logged in to vote
1 reply
@groue
Comment options

Answer selected by mallman
Comment options

You must be logged in to vote
3 replies
@groue
Comment options

@mallman
Comment options

mallman Jun 16, 2023
Collaborator Author

@groue
Comment options

Comment options

mallman
Jun 21, 2023
Collaborator Author

You must be logged in to vote
2 replies
@groue
Comment options

@mallman
Comment options

mallman Jun 22, 2023
Collaborator Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants