[英]Npgsql.PostgresException: '42P01: relation “table” does not exist'
[英]Npgsql: prepared statement “_p1” does not exist
我正在尝试使用此方法将EOD库存数据加载到表中:
public async Task<long> BulkInsertEodData(IEnumerable<EodData> records, string symbol)
{
var recordsProcessed = 0L;
using (var conn = await OpenConnection())
using (var trans = conn.BeginTransaction())
using (var comm = _factory.CreateCommand())
{
try
{
comm.Connection = conn;
comm.Transaction = trans;
comm.CommandText = INSERT_EOD;
var ps = AddParametersToInsertEodQuery(comm);
foreach (var p in ps) comm.Parameters.Add(p);
comm.Prepare();
foreach (var record in records)
{
comm.Parameters["@date_id"].Value = record.DateId;
comm.Parameters["@symbol"].Value = symbol.ToUpper();
comm.Parameters["@eod_close"].Value = record.EodClose;
comm.Parameters["@eod_high"].Value = record.EodHigh;
comm.Parameters["@eod_low"].Value = record.EodLow;
comm.Parameters["@eod_volume"].Value = record.EodVolume;
comm.Parameters["@eod_open"].Value = record.EodOpen;
comm.Parameters["@eod_split"].Value = record.EodSplit;
comm.Parameters["@eod_dividend"].Value = record.EodDividend;
comm.Parameters["@last_modified"].Value = DateTime.UtcNow;
await comm.ExecuteNonQueryAsync();
recordsProcessed++;
}
trans.Commit();
}
catch (Exception ex)
{
_logger.LogError(ex, "BulkInsertEodData(IEnumerable<EodData>)");
trans.Rollback();
}
}
return recordsProcessed;
}
查询文本如下:
INSERT INTO public.eod_datas(
date_id,
stock_id,
eod_open,
eod_close,
eod_low,
eod_high,
eod_volume,
eod_dividend,
eod_split,
last_modified_timestamp
)
values
@date_id,
(select s.id from stocks s where s.symbol = @symbol limit 1),
@eod_open,
@eod_clos,
@eod_low,
@eod_high,
@eod_volume,
@eod_dividend,
@eod_split,
current_timestamp
on conflict (date_id, stock_id)
do update set
eod_open = @eod_open,
eod_close = @eod_close,
eod_low = @eod_low,
eod_high = @eod_high,
eod_volume = @eod_volume,
eod_dividend = @eod_dividend,
eod_split = @eod_split,
last_modified_timestamp = current_timestamp;
这不是我第一次使用准备好的语句,但这次我做了一些不同的事情(.NET Core,使用DbProviderFactory),并且得到了奇怪的结果。
通过此方法的前几次,我收到Npgsql.PostgresException (0x80004005): 42601: syntax error at or near "$1"
的影响的错误Npgsql.PostgresException (0x80004005): 42601: syntax error at or near "$1"
本身是相当神秘的,但最神秘的是该错误几个方法调用后实际上消失了 ,并且我开始得到Npgsql.PostgresException (0x80004005): 26000: prepared statement "_p1" does not exist
一致的后缀。
有人可以解释这种行为吗? 我究竟做错了什么? 在哪里可以获取有关“ $ 1”的更多详细信息?
您缺少插入值周围的括号。 可悲的是,Postgres不会告诉您它期望$ 1之前有括号。
正确的语法:
values (
@date_id,
(select s.id from stocks s where s.symbol = @symbol limit 1),
@eod_open,
@eod_clos,
@eod_low,
@eod_high,
@eod_volume,
@eod_dividend,
@eod_split,
current_timestamp)
on conflict (date_id, stock_id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.