←back to thread

620 points tambourine_man | 1 comments | | HN request time: 0.259s | source
Show context
TekMol ◴[] No.43749608[source]
Will this allow neat SQL syntax like the following?

    city = 'London'
    min_age = 21
    # Find all users in London who are 21 or older:
    users = db.get(t'
        SELECT * FROM users
        WHERE city={city} AND age>{min_age}
    ')
If the db.get() function accepts a template, it should, right?

This would be the nicest way to use SQL I have seen yet.

replies(8): >>43749674 #>>43749734 #>>43749906 #>>43749926 #>>43749979 #>>43750037 #>>43751845 #>>43756963 #
zelphirkalt ◴[] No.43751845[source]
Isn't the actually proper way to use prepared statements anyway? If we are doing that properly, then what does this t string business buy us for SQL usage from Python?
replies(1): >>43753528 #
scott_w ◴[] No.43753528[source]
Because, as the article states, people aren’t using prepared statements. Instead, they pass f-strings because they’re more convenient.
replies(2): >>43754166 #>>43755093 #
1. hedgehog ◴[] No.43754166[source]
f strings are syntax rather than a type, the resulting templates look like a reasonable way to specify a prepared statement.