如何在PostgreSQL表中插入包含序列的记录?

时间:2015-10-28 17:04:10

标签: postgresql npgsql

我想在一个带有序列号“ID”的表中添加行。

我不需要为in提供ID来自动增加,但我似乎无法将其删除:

       NpgsqlCommand cmd = new NpgsqlCommand("insert into \"Min_Bar_Price_Data\" values(:SEC_ID, :PX_OPEN, :PX_HIGH, :PX_LOW, :PX_LAST, :PX_VOLUME, :Date)", conn);
        //cmd.Parameters.Add(new NpgsqlParameter("ID", DbType.Int32));
        cmd.Parameters.Add(new NpgsqlParameter("SEC_ID", DbType.Int32));
        cmd.Parameters.Add(new NpgsqlParameter("PX_OPEN", DbType.Double));
        cmd.Parameters.Add(new NpgsqlParameter("PX_HIGH", DbType.Double));
        cmd.Parameters.Add(new NpgsqlParameter("PX_LOW", DbType.Double));
        cmd.Parameters.Add(new NpgsqlParameter("PX_LAST", DbType.Double));
        cmd.Parameters.Add(new NpgsqlParameter("PX_VOLUME", DbType.Double));
        cmd.Parameters.Add(new NpgsqlParameter("Date", DbType.DateTime));

这是表格:

CREATE TABLE "Min_Bar_Price_Data"
(
  "ID" integer NOT NULL DEFAULT nextval('"Price_Data_ID_seq"'::regclass),
  "Sec_ID" integer NOT NULL,
  "PX_OPEN" double precision,
  "PX_HIGH" double precision,
  "PX_LOW" double precision,
  "PX_LAST" double precision,
  "PX_VOLUME" double precision,
  "Date" timestamp without time zone NOT NULL,
  CONSTRAINT "Min_Bar_Price_Data_pkey" PRIMARY KEY ("Sec_ID", "Date")
)
WITH (
  OIDS=FALSE
);

然后我填写参数:

            //cmd.Parameters["ID"].Value = ID;
            cmd.Parameters["SEC_ID"].Value = sec_ID;
            cmd.Parameters["PX_OPEN"].Value = 0.0;
            cmd.Parameters["PX_HIGH"].Value = 0.0;
            cmd.Parameters["PX_LOW"].Value = 0.0;
            cmd.Parameters["PX_LAST"].Value = d.Close;
            cmd.Parameters["PX_VOLUME"].Value = 1.0;
            cmd.Parameters["Date"].Value = d.DT;

                cmd.ExecuteNonQuery();

{“错误:42804:列\”PX_VOLUME \“的类型为double precision,但表达式的类型为timestamp,没有时区”}

1 个答案:

答案 0 :(得分:0)

您应该明确指定列列表:

 NpgsqlCommand cmd = new NpgsqlCommand(
      "INSERT INTO \"Min_Bar_Price_Data\"" +
      "(\"Sec_ID\", 
        \"PX_OPEN\", 
        \"PX_HIGH\", 
        \"PX_LOW\", 
        \"PX_LAST\", 
        \"PX_VOLUME\", 
        \"Date\")" +
      "VALUES" +
      "(:SEC_ID, 
        :PX_OPEN, 
        :PX_HIGH, 
        :PX_LOW, 
        :PX_LAST, 
        :PX_VOLUME, 
        :Date)", conn);