Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Return Row ID postgresql
#1
Hi,

I make a match between 2 tables in a database and I want to use the ID's of the rows where the matches are made. 
Sometimes this works correct and i get the different row numbers returned, but not always. 

I have boiled down the problem to two examples:

Match 1: CORRECT Row return
(here i match amounts. There are 3 positive matches, for which the corresponding row id's are returned)

def testrowid2():  # from db table that is set as float
    for a in amount_to_pay:
        if a in amount_paid:
                cur.execute("select participant_id from participants WHERE amount_to_pay = (%s)", (a,))
                row = cur.fetchone()
                print(row)
Output:
('1',) ('2',) ('4',)
Match 2: INCORRECT row return
(here I match dates. For all selections there is a match, so I expect the ID's of all row nrs as output of this definition. Instead i get the ID nr of the first row ('6') printed for the number of rows where the match has been made.


def testrowid1():
    for d in date:
        if d in paid_date:
                cur.execute("select participant_id from participants WHERE participant_date = (%s)", (d,))
                row = cur.fetchone()
                print(row)
Output:
('6',) ('6',) ('6',) ('6',) ('6',) ('6',) etc etc
Any ideas on how I can always get the correct row id nr?
Reply
#2
you can get ctid (same as rowid), see: https://www.postgresql.org/docs/8.3/stat...lumns.html
but be aware, it is volatile. You are better off to append a sequence number to the tables that
you need this for, as it will alwys be the same. See: http://www.neilconway.org/docs/sequences/
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to Connect to PostgreSQL Through Jump Server and SSH Tunnel using Python? nishans 1 986 Jan-02-2024, 10:37 AM
Last Post: khanzain
  maintain a postgresql database using osm2pgsql apollo 1 2,310 Aug-03-2020, 10:33 PM
Last Post: Larz60+
  PostgreSQL psycopg2.errors.DuplicateColumn: column specified more than once rajnish_nationfirst 2 3,788 Jun-21-2020, 08:17 AM
Last Post: ibreeden
  StopIteration exception when mock PostgreSQL connection in several tests igor87z 1 2,920 Jun-10-2020, 06:16 PM
Last Post: ibreeden
  Python and Postgresql syntax select statement Nesreenmhd 1 4,989 Sep-07-2019, 06:08 PM
Last Post: ndc85430
  Create table with psycopg2 on postgreSQL DB yhecohen 2 3,327 Aug-23-2019, 05:56 AM
Last Post: massimo_m
  Error while fetching data from PostgreSQL linu 3 4,314 May-13-2019, 02:38 PM
Last Post: rxndy
  PostgreSQL- import package elhetch 2 4,742 Jun-07-2017, 02:07 PM
Last Post: snippsat
  Nginx Setup Django Postgresql Eclipse Adelton 3 4,136 Mar-27-2017, 09:40 PM
Last Post: snippsat
  Postgresql and Django Configuration with Wamp Adelton 5 5,690 Mar-26-2017, 07:15 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020