Chris Stryczynski
Chris Stryczynski

Reputation: 33891

SqlError due to single quotes around parameterized value for `CREATE DATABASE`

I'm using the "postgresql-simple" library and running into a SqlError with the following:

_ <- execute con "CREATE DATABASE ?" (Only ("example" :: String))

Results in an error of:

SqlError {sqlState = "42601", sqlExecStatus = FatalError, sqlErrorMsg = "syntax error at or near \"'example'\"", sqlErrorDetail = "", sqlErrorHint = ""}

With the db logs being:

[postgres-db-b6f75998b-5qnnx db] 2019-12-28 12:28:40.062 UTC [100] ERROR:  syntax error at or near "'example'" at character 17 
[postgres-db-b6f75998b-5qnnx db] 2019-12-28 12:28:40.062 UTC [100] STATEMENT:  CREATE DATABASE 'example' 

Why are single quotes being added around the value?

Is this possibly due to postgresql-simple not using prepared statements?

Upvotes: 0

Views: 169

Answers (1)

Chris Stryczynski
Chris Stryczynski

Reputation: 33891

According to https://nikita-volkov.github.io/hasql-benchmarks/

Unlike “postgresql-simple”, the library utilizes prepared statements, which allows the database to parse the SQL statements only once per connection.

So I wrongly assumed postgresql-simple would be using prepared statements, and hence it'll add single quotes around seemingly any string value.

Upvotes: 0

Related Questions