Python Postgres - psycopg2.ProgrammingError:没有获取结果

时间:2017-06-01 08:59:52

标签: python postgresql psycopg2

我一直坚持这一点。我是postgres的新手。 postgres中的CRUD操作太奇怪了。 我无法获取最新插入行的ID 我在cursor.fetchone()[0]

之后使用了cur.execute()

我的错误是: -

   Traceback (most recent call last):
  File "main.py", line 79, in <module>
    shipping_bill_id = cur.fetchone()[0]
psycopg2.ProgrammingError: no results to fetch

我想获取最新的插入ID,以便在我想要加入的两个表之间建立关系。 我的SQL架构是: -

  create_table "companies", force: :cascade do |t|
    t.string   "iec"
    t.string   "party_name"
    t.boolean  "added_to_crm"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.boolean  "is_active",       default: true
    t.datetime "last_updated_at"
  end
  create_table "shipping_bills", force: :cascade do |t|
    t.integer  "company_id"
    t.float    "fob_value"
    t.string   "fob_currency"
    t.string   "origin_port_name"
    t.string   "destination_port_name"
    t.integer  "origin_port_code"
    t.integer  "destination_port_code"
    t.string   "invoice_number"
    t.string   "shipping_bill_number"
    t.datetime "created_at",            null: false
    t.datetime "updated_at",            null: false
  end
  create_table "shipping_bills_products", force: :cascade do |t|
    t.integer  "shipping_bill_id"
    t.float    "total_price"
    t.text     "name"
    t.float    "unit_price"
    t.string   "unit_currency"
    t.string   "hss_code"
    t.integer  "quantity"
    t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
  end

我的代码: -

    data = {    "products" :
                            {
                              "name": "",
                              "hs_code": "",
                              "unit_price": "",
                              "unit_currency": "",
                              "quantity": "",
                              "total_price": ""
                            }
        }

data["FOB_currency"] = obj["Currency "]
data["origin_port_name"] = "MUNDRA"
data["destination_port_name"] = obj["Port of Discharge"]
data["origin_port_code"] = "INMUN1"
data["destination_port_code"] = get_port_code(obj["Port of Discharge"])
iec = str(int(obj["IEC"]))
if len(iec) == 9 :
    data["IEC"] = "0"+iec
else:
    data["IEC"] = iec

data["products"]["quantity"]  = obj["Quantity "]
data["products"]["unit_price"] = obj["Item rate "]
data["products"]["name"] = obj["Item Desc "]
data["products"]["hs_code"] = int(obj["RITC Code "])
data["products"]["unit_currency"] = obj["Currency "]
data["FOB_value"] = obj["FOB "]
data["shipping_bill_date"] = obj["SB.Date"]
data["shipping_bill_no"] = int(obj["SB.No."])
data["invoice_number"] = obj["Invoice No "]

company_id = None
for e in company_rows:
    if e[1] == data["IEC"]:
        company_id = e[0]

if company_id != None :
    cur.execute("INSERT INTO shipping_bills (company_id,fob_value,fob_currency,origin_port_name,destination_port_name,origin_port_code,destination_port_code,invoice_number,shipping_bill_number,created_at,updated_at) \
                 VALUES  (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",(company_id,data["FOB_value"],str(data["FOB_currency"]),data["origin_port_name"],data["destination_port_name"],data["origin_port_code"],data["destination_port_code"],data["invoice_number"],data["shipping_bill_no"],datetime.datetime.now(),datetime.datetime.now())) ;

    shipping_bill_id = cur.fetchone()[0]
    total_price = data["products"]["unit_price"]*data["products"]["quantity"]
    cur.execute("INSERT INTO shipping_bills_products (shipping_bill_id,total_price,name,unit_price,unit_currency,hss_code,quantity)  \
                VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)",(shipping_bill_id,total_price,str(data["products"]["name"]),data["products"]["unit_price"],str(data["products"]["unit_currency"]),data["products"]["hs_code"],data["products"]["quantity"],datetime.datetime.now(),datetime.datetime.now())) ;

    conn.commit()
    print(company_id)

2 个答案:

答案 0 :(得分:0)

您遗失returning

cur.execute('''
    INSERT INTO shipping_bills (
        company_id,
        fob_value,
        fob_currency,
        origin_port_name,
        destination_port_name,
        origin_port_code,
        destination_port_code,
        invoice_number,
        shipping_bill_number,
        created_at,
        updated_at
    ) values  (%s,%s,%s,%s,%s,%s,%s,%s,%s,current_timestamp,current_timestamp)
    returning shipping_bill_number
    ''', (
        company_id,data["FOB_value"],
        str(data["FOB_currency"]),
        data["origin_port_name"],
        data["destination_port_name"],
        data["origin_port_code"],
        data["destination_port_code"],
        data["invoice_number"],
        data["shipping_bill_no"]
    )
) ;

答案 1 :(得分:0)

对于遇到此问题的其他人:

我遇到了类似的问题,即在fetchone()上针对“选择”语句获取此异常:

cursori = conn.cursor()
cursori.execute("select * from table where col = 'string'")
while 1:
    data = cursori.fetchone()
    if not data:
        break

    # other code...

原来,我犯了一个简单的错误,即在“其他代码”中使用“ cursori”执行另一个查询。