英文:
quoted parameters to golang sqlc
问题
使用sqlc生成数据库方法。以下是要翻译的查询:
-- name: RemoveRows:exec
DELETE FROM demotable
WHERE last_updated < (CURRENT_TIMESTAMP - INTERVAL '30' SECOND);
我想将30
作为参数传递,但是我使用了以下查询,然后引号没有出现,数据库报错。
-- name: RemoveRows:exec
DELETE FROM demotable
WHERE last_updated < (CURRENT_TIMESTAMP - INTERVAL $1 SECOND);
尝试引用$1
,但是sqlc认为它是字符串。
-- name: RemoveRows:exec
DELETE FROM demotable
WHERE last_updated < (CURRENT_TIMESTAMP - INTERVAL '$1' SECOND);
英文:
using sqlc to generate db methods.
Have below query
-- name: RemoveRows:exec
DELETE FROM demotable
WHERE last_updated < (CURRENT_TIMESTAMP - INTERVAL '30' SECOND);
I want to pass 30
as parameter but I used below query, then quotes are not coming and db giving error.
-- name: RemoveRows:exec
DELETE FROM demotable
WHERE last_updated < (CURRENT_TIMESTAMP - INTERVAL $1 SECOND);
Tried quoting $1
but sqlc thinking it is string
-- name: RemoveRows:exec
DELETE FROM demotable
WHERE last_updated < (CURRENT_TIMESTAMP - INTERVAL '$1' SECOND);
答案1
得分: 2
或者,而不是在尝试欺骗类型系统时纠缠不清,只需执行以下操作:
... - ($1 * interval '1 minute')
英文:
Or rather than mucking around trying to trick the type system just do
... - ($1 * interval '1 minute')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论