英文:
Syntax error at end of input in PostgreSQL
问题
我在MySQL和PostgreSQL中都使用了下面的SQL语句,但在PostgreSQL中失败了
db.Query(`SELECT COUNT(*) as N FROM email WHERE address = ?`, email)
出现了以下错误:
pq: F:"scan.l" M:"syntax error at end of input" S:"ERROR" C:"42601" P:"50" R:"scanner_yyerror" L:"993"
问题是什么?PostgreSQL中的错误消息非常晦涩。
英文:
I have used the next SQL statement in both MySQL and PostgreSQL, but it fails in PostgreSQL
db.Query(`SELECT COUNT(*) as N FROM email WHERE address = ?`, email)
with this error:
pq: F:"scan.l" M:"syntax error at end of input" S:"ERROR" C:"42601" P:"50" R:"scanner_yyerror" L:"993"
What's the problem? The error messages in PostgreSQL are very cryptic.
答案1
得分: 86
你没有提供关于语言/环境的任何细节,但我会尝试一个猜测:
MySQL的预处理语句本地使用?
作为参数占位符,但是PostgreSQL使用$1
,$2
等。尝试用$1
替换?
,看看是否有效:
WHERE address = $1
> PostgreSQL中的错误消息非常晦涩。
总的来说,我发现Postgres的错误消息比竞争产品(咳咳,MySQL和尤其是Oracle)要好,但在这种情况下,您已经使解析器混乱到了疯狂的程度。
英文:
You haven't provided any details about the language/environment, but I'll try a wild guess anyway:
MySQL's prepared statements natively use ?
as the parameter placeholder, but PostgreSQL uses $1
, $2
etc. Try replacing the ?
with $1
and see if it works:
WHERE address = $1
> The error messages in PostgreSQL are very cryptic.
In general, I've found that Postgres error messages are better than competing products (ahem, MySQL and especially Oracle), but in this instance you've managed to confuse the parser beyond sanity.
答案2
得分: 12
在golang中,对于查询,我们使用以下方式:
- MySQL使用?变体
- PostgreSQL使用枚举的$1,$2等绑定变量语法
- SQLite同时接受?和$1语法
- Oracle使用:name语法
英文:
In golang for queries we have use
- MySQL uses the ? variant
- PostgreSQL uses an enumerated $1, $2, etc bindvar syntax
- SQLite accepts both ? and $1 syntax
- Oracle uses a :name syntax
答案3
得分: 8
你正在使用Go对吗?
尝试一下:
db.Query(`SELECT COUNT(*) as N FROM email WHERE address = $1`, email)
英文:
You are using Go right?
try:
db.Query(`SELECT COUNT(*) as N FROM email WHERE address = $1`, email)
答案4
得分: 6
在我的情况下,这是因为我使用了一个 -- 行注释,而负责与数据库交互的程序将我的查询的多行读取为一行。这意味着行注释破坏了查询的其余部分。解决方法是使用 /* 块注释 */。
英文:
In my case, it was due to using a -- line comment where the program that was responsible for interacting with the database read in the multiple lines of my query all as one giant line. This meant that the line comment corrupted the remainder of the query. The fix was to use a /* block comment */ instead.
答案5
得分: 1
另一种可能性是检查是否缺少了IF
、LOOP
等的END
指令。
我在一个循环中工作了很长时间,以至于没有意识到我从未加入END LOOP;
。
Postgres显示的错误信息:
ERROR: syntax error at end of input
LINE 123: $$;
对于告诉你某个结构是否未关闭,这个错误信息非常无用。
英文:
Another possibility - check for any missing END
directives for IF
, LOOP
, etc.
I'd been working in a loop so long, that I didn't realize I never put in an END LOOP;
.
The message that Postgres displays:
ERROR: syntax error at end of input
LINE 123: $$;
is terribly unhelpful at telling you if some construct was never closed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论