fetchall() psycopg2 returning empty list

Viewed 304

I'm trying to make a query using psycopg2. I know the query below works when I query it in pgAdmin, but for some reason retchall() is returning an empty list.

I've used the same formatting before in other queries and been able to fetch the results so I'm wodnering if there's a formatting issue or something. I don't get any errors when executing it just returns an empty list.

I'm also using DictCursor so I can reference the query by keys: https://www.psycopg.org/docs/extras.html#dictionary-like-cursor

    import db_funcs.DB as DB
    import time
    import psycopg2.extras
    import json
    import sys
    
    def route_export(stops, project_list):
        try:
            cursor, connection = DB.dbInit()
    
            dict_cur = connection.cursor(cursor_factory=psycopg2.extras.DictCursor)
    
            for project in project_list:
                drivers = []
                stops = {"stops": stops[project]}
    
                dict_cur.execute(
                    """SELECT rp.route_project_id, rp.route_name, rp.driver_id, rp.route_date,
                    d.driver_id, d.name as driver_name, d.start_location_coords, 
                    d.start_location_add, d.end_location_coords, d.end_location_add, 
                    d.shift_start, d.shift_end, d.phone_number, d.speed, d.capacity, d.types, 
                    d.breaks, d.delivery_area_id 
                    FROM route_project rp
                    JOIN driver d 
                    ON rp.driver_id = d.driver_id
                    WHERE TO_DATE(LEFT(route_date,10),'YYYY-MM-DD') >= NOW()
                    and route_name = %s """,
                    (project,),
                )
    
                rows = dict_cur.fetchall()
1 Answers

Can you try this running, see if you get output?

import db_funcs.DB as DB
import time
import psycopg2.extras
import json
import sys

def route_export(stops, project_list):
try:
    cursor, connection = DB.dbInit()

    dict_cur = connection.cursor(cursor_factory=psycopg2.extras.DictCursor)

    for project in project_list:
        drivers = []
        project = str(project)
        stops = {"stops": stops[project]}

        q = dict_cur.execute(
            'SELECT rp.route_project_id, rp.route_name, rp.driver_id, rp.route_date,
            d.driver_id, d.name as driver_name, d.start_location_coords, 
            d.start_location_add, d.end_location_coords, d.end_location_add, 
            d.shift_start, d.shift_end, d.phone_number, d.speed, d.capacity, d.types, 
            d.breaks, d.delivery_area_id 
            FROM route_project rp
            JOIN driver d 
            ON rp.driver_id = d.driver_id
            WHERE TO_DATE(LEFT(route_date,10),"YYYY-MM-DD") >= NOW()
            and route_name = ?', [project])

        rows = q.fetchall()
Related