SQL PostgreSQL 输入结束时的语法错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13119786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 11:49:53  来源:igfitidea点击:

Syntax error at end of input in PostgreSQL

sqlpostgresqlgo

提问by intgr

I have used the next SQL statement in both MySQL and PostgreSQL, but it fails in PostgreSQL

我在 MySQL 和 PostgreSQL 中都使用了下一个 SQL 语句,但它在 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.

有什么问题?PostgreSQL 中的错误消息非常神秘。

回答by intgr

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, $2etc. Try replacing the ?with $1and see if it works:

MySQL的准备语句本身使用?作为参数占位符,而PostgreSQL使用$1$2等尝试更换?$1,看看它的工作原理:

WHERE address = 

The error messages in PostgreSQL are very cryptic.

PostgreSQL 中的错误消息非常神秘。

In general, I've found that Postgres error messages are better than competing products (ahem, MySQL and especiallyOracle), but in this instance you've managed to confuse the parser beyond sanity. :)

总的来说,我发现 Postgres 错误消息比竞争产品(啊哈,MySQL,尤其是Oracle)要好,但是在这种情况下,您已经设法使解析器变得异常混乱。:)

回答by simonmenke

You are using Go right?

你在用 Go 对吗?

try:

尝试:

db.Query(`SELECT COUNT(*) as N FROM email WHERE address = `, email)

回答by refrazul

In golang for queries we have use

在 golang 中,我们使用了查询

  • MySQL uses the ? variant
  • PostgreSQL uses an enumerated $1, $2, etc bindvar syntax
  • SQLite accepts both ? and $1 syntax
  • Oracle uses a :name syntax
  • MySQL 使用 ? 变体
  • PostgreSQL 使用枚举的 $1、$2 等 bindvar 语法
  • SQLite 接受两者?和 $1 语法
  • Oracle 使用 :name 语法

回答by calamari

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.

就我而言,这是由于使用了 -- 行注释,其中负责与数据库交互的程序将我的查询的多行读取为一条巨行。这意味着行注释破坏了查询的其余部分。修复方法是使用 /* 块注释 */ 代替。