【问题标题】:call store procedure using callproc do not really implement in postgresql?使用 callproc 调用存储过程并没有真正在 postgresql 中实现?
【发布时间】:2020-12-27 13:50:41
【问题描述】:

我有 python 3.7 和 postgresql:

import psycopg2
...
@app.route('/add', methods=['POST'])
def add():
    cur = get_db().cursor()
    if request.is_json:
        jdata = request.get_json()
        cur.callproc("add", (jdata['tools'], jdata['ip_begin'], jdata['ip_end']))
        result = cur.fetchall()
        print(result)
        cur.close()
        return jsonify({'result': 'add successful'})
    else:
        cur.close()
        return jsonify({'error':"invalid input json data"})

我的店铺功能是:

CREATE FUNCTION "public"."add"("tool_name" varchar, "ip_bgn" inet, "ip_end" inet) RETURNS "pg_catalog"."int8"
$$  
DECLARE
mid INTEGER := 0;
ins BIGINT := 0;
BEGIN
  if ip_bgn < ip_end then
INSERT into ip_addresses(tool_name, ip_start, ip_end) VALUES (tool_name, ip_bgn, ip_end) RETURNING id into mid;
    Loop
        if ip_bgn + ins >= ip_end then
            exit;
        end if;
        INSERT into ip_pools(tool_name, ip_address, to_main, status) VALUES(tool_name, ip_bgn+ins, mid, 'free');
        ins := ins + 1;
    end Loop;
end if;
RETURN ins;
END $$

我的输出是:

 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 [(11,)]                   ######################## I have return values
 127.0.0.1 - - [28/Dec/2020 00:37:40] "POST /add HTTP/1.1" 200 -

但是当我去数据库时,我发现我的python代码执行后数据库没有添加任何行。

数据库和我之前执行的一样。

我可以使用 plpgsql 控制台通过相同的存储功能创建真实数据。但是如果我通过python代码运行它并不会真正添加行和更改表,但返回数据是可以的。

【问题讨论】:

    标签: python postgresql stored-procedures psycopg2


    【解决方案1】:

    您不会在任何地方提交对数据库所做的更改。假设你的连接是这样定义的(好像是get_db,以后可以改):

    conn = psycopg2.connect(host="db.host.com", user="username", password="secret")
    

    关闭光标后需要提交修改:

    cur.callproc("add", (jdata['tools'], jdata['ip_begin'], jdata['ip_end']))
    # some code
    cur.close()
    conn.commit()
    # close the connection as well once done
    conn.close()
    

    来自pycopg2 documentation

    请注意,关闭连接而不首先提交更改将导致任何挂起的更改被丢弃,就像执行了 ROLLBACK 一样(除非选择了不同的隔离级别:请参阅 set_isolation_level())。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-02
      • 1970-01-01
      • 2016-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多