[英]PostgreSQL, Npgsql returning 42601: syntax error at or near “$1”
I'm trying to use Npgsql and/or Dapper to query a table and I keep running into Npgsql.PostgresException 42601: syntax error at or near "$1".
我正在尝试使用Npgsql和/或Dapper来查询表,并且我继续
Npgsql.PostgresException 42601: syntax error at or near "$1".
Here is what I've got trying it with NpgsqlCommand: 这是我用NpgsqlCommand尝试的:
using (var conn = new NpgsqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["postgres"].ConnectionString))
{
conn.Open();
using (NpgsqlCommand command = new NpgsqlCommand("select * from Logs.Logs where Log_Date > current_date - interval @days day;", conn))
{
command.Parameters.AddWithValue("@days", days);
var reader = command.ExecuteReader();
I've also tried it with Dapper(my preferred method) with: 我也用Dapper(我的首选方法)尝试过:
using (var conn = new NpgsqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["postgres"].ConnectionString))
{
conn.Open();
var logs = conn.Query<Log>("select * from Logs.Logs where Log_Date > current_date - interval @days day;", new {days = days});
Either way I get the same Npgsql.PostgresException 42601: syntax error at or near "$1" error.
无论哪种方式,我得到相同的
Npgsql.PostgresException 42601: syntax error at or near "$1" error.
The Statement in the Exception shows: select * from Logs.Logs where Log_Date > current_date - interval $1 day
异常中的语句显示:
select * from Logs.Logs where Log_Date > current_date - interval $1 day
Note, if I do the following it works fine, but it's not properly parameterized: 请注意,如果我执行以下操作它可以正常工作,但它没有正确参数化:
var logs = conn.Query<Log>("select * from Logs.Logs where Log_Date > current_date - interval '" + days + "' day;");
What am I doing wrong? 我究竟做错了什么? I very much appreciate any feedback.
我非常感谢任何反馈。 Thank you.
谢谢。
PostgreSQL doesn't allow you to stick a parameter anywhere in a query. PostgreSQL不允许您在查询中的任何位置粘贴参数。 What you want can be achieved with the following:
您可以通过以下方式实现您的目标:
var command = new NpgsqlCommand("select * from Logs.Logs where Log_Date > current_date - @days", conn))
command.Parameters.AddWithValue("@days", TimeSpan.FromDays(days));
This way you're passing the interval directly from Npgsql to PostgreSQL, rather than a part of the expression designed to create that interval. 这样您就可以将间隔从Npgsql直接传递给PostgreSQL,而不是用于创建该间隔的表达式的一部分。
To subtract days from a date (assuming log_date
is data type date
), you can simplify: 要从日期中减去天数(假设
log_date
是数据类型date
),您可以简化:
"SELECT * FROM logs.logs WHERE log_date > CURRENT_DATE - @days;"
And provide @days
as unquoted numeric literal (digits only) - which is taken to be an integer
. 并将
@days
提供为不带引号的数字文字(仅限数字) - 它被视为一个integer
。 This is even more efficient, since date
- integer
returns date
, while date
- interval
returns timestamp
. 这更有效,因为
date
- integer
返回date
,而date
- interval
返回timestamp
。
i got this error using DapperExtensions 我使用DapperExtensions得到了这个错误
adding 加入
DapperExtensions.DapperExtensions.SqlDialect = new PostgreSqlDialect();
DapperAsyncExtensions.SqlDialect = new PostgreSqlDialect();
before creating the connection fixed the issue 在创建连接之前修复了问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.