【问题标题】:Cannot figure out why query is failing无法弄清楚查询失败的原因
【发布时间】:2015-12-17 02:38:09
【问题描述】:

我正在尝试迁移一些将数据插入数据库的代码,以便继续使用 postgres。下面的代码/查询失败了,在查看了数组的大小之后,它似乎应该可以工作了。

任何关于失败原因的意见都会很棒。

我的 SQL 的样子:

sql = "insert into postgres.options.options (delta,gamma,rho,theta,\"impVol\",value,vega,date,ticker,\"callPut\",\"Chg\",\"maturity\",\"Symbol\",\"Strike\",\"Implied\",\"Last\",\"Vol\",\"Ask\",\"Bid\") values " \
      "(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"

我的数据看起来像这样:

data=[0.782154229511169, 0.026557152004256603, 0.005658995525391192, -0.4087164010990902, 136.71875, 8.94733166658628, 0.02387435675989819, 20150706, u'SVXY', u'C', -2.9, 20150710, u'SVXY150710C00070000', 70.0, 0.0, 7.9, 5.0, 9.2, 8.7]

我的python代码如下:

       cur.execute(sql,data)
        conn.commit()

我在桌子上的创建语句如下:

CREATE TABLE options.options
(
  delta double precision,
  gamma double precision,
  rho double precision,
  theta double precision,
  "impVol" double precision,
  value double precision,
  vega double precision,
  _id text,
  date bigint,
  ticker text,
  "callPut" text,
  "Chg" double precision,
  maturity integer,
  "Symbol" text,
  "Strike" double precision,
  "Implied" double precision,
  "Last" double precision,
  "Vol" double precision,
  "Ask" double precision,
  "Bid" double precision
)

我的错误是:

Traceback (most recent call last):
  File "C:/Users/jasonmellone/PycharmProjects/price_options_multithread/migrate_error_options.py", line 41, in <module>
    cur.execute(sql,data)
InternalError: current transaction is aborted, commands ignored until end of transaction block

【问题讨论】:

    标签: python postgresql psycopg2


    【解决方案1】:

    您的插入语句中有错误。 postgres.options.options 不是一个有效的表,因为 PostgreSql 认为,你的意思是对另一个数据库的引用。改变这个:

    insert into postgres.options.options...
    

    到这里:

    insert into options.options...
    

    【讨论】:

      【解决方案2】:

      您已经在options 架构中创建了一个表options。而您使用 postgres 模式插入。

      更改插入:

      sql = "insert into options.options  ..."
      

      【讨论】:

        【解决方案3】:

        在 python 中构建我的查询字符串,通过使用 SQLAlchemy,我能够使用以下逻辑插入:

        def get_engine():
            engine = create_engine('postgresql+psycopg2://user:password@localhost/postgres')
            return engine
        
        db = get_engine()
        connection = db.connect()
        connection.execute(sql)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-05-19
          • 1970-01-01
          • 2014-10-08
          • 1970-01-01
          • 1970-01-01
          • 2018-04-29
          相关资源
          最近更新 更多