Syntax error at end of input in PostgreSQL

后端 未结 4 2026
南旧
南旧 2020-11-29 11:35

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 = ?`, ema         


        
相关标签:
4条回答
  • 2020-11-29 11:38

    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. :)

    0 讨论(0)
  • 2020-11-29 11:40

    You are using Go right?

    try:

    db.Query(`SELECT COUNT(*) as N FROM email WHERE address = $1`, email)
    
    0 讨论(0)
  • 2020-11-29 11:56

    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.

    0 讨论(0)
  • 2020-11-29 11:57

    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
    0 讨论(0)
提交回复
热议问题