LoginSignup
1
0

More than 3 years have passed since last update.

psycopg2でpsycopg2.ProgrammingError: no results to fetchと言われたとき

Posted at

statusmessage

fetchall()やfetchone()ではなくstatusmessageを使う。
select文だとfetchall()で結果を表示できるが、insert文だとエラーを吐く。

import psycopg2


def insert_values(name, age):
    conn = psycopg2.connect(host=HOST,
                            user=USER,
                            password=PASSWORD,
                            dbname=DBNAME,
                            port=PORT)
    cur = conn.cursor()
    cur.execute('INSERT INTO sample_table VALUES(%s, %s)', (name, age))
    cur.execute(sql)
    conn.commit()
    print(cur.statusmessage)


if __name__ == "__main__":
    insert_values('Taro', 5)

参考記事

stackoverflow
https://stackoverflow.com/questions/55400707/python3-psycopg2-no-results-to-fetch-using-returning

github issues
https://github.com/psycopg/psycopg2/issues/346

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0