Re: New user questions - Mailing list psycopg

From Adrian Klaver
Subject Re: New user questions
Date
Msg-id b030d87b-43d9-d994-7291-18a0caef0656@aklaver.com
Whole thread Raw
In response to Re: New user questions  (Rich Shepard <rshepard@appl-ecosys.com>)
Responses Re: New user questions  (Adrian Klaver <adrian.klaver@aklaver.com>)
Re: New user questions  (Rich Shepard <rshepard@appl-ecosys.com>)
List psycopg
On 7/14/21 10:35 AM, Rich Shepard wrote:
> On Tue, 13 Jul 2021, Adrian Klaver wrote:
> 
>> Then you want to take a look at this:
>> https://www.psycopg.org/docs/sql.html
> 
> Adrian,
> 
> Based on pages 1-2 of that doc I've re-written one each of an insert and
> select query. When they are correct I'll do the rest of the insert and
> select queries.
> 
> Query 1:
> cur.execute(
>      sql.SQL("insert into {} values (%s, %s, %s, %s, %s)"
>          .format(sql.identifier('contacts')),
>          [c.nbr, a.date, a.type, a.notes, a.next]))

Where are c.nbr and a.* coming from? If they are supposed to be table 
qualified values this will not work.

Also it is sql.Identifier.


> 
> Query 2:
> query = sql.SQL("select {fields} from {tables}").format(
>        fields = sql.SQL(',').join([
>        sql.identifier('p.lname'),
>        sql.identifier('p.fname'),
>        sql.identifier('p.loc_nbr'),
>        sql.identifier('p.job_title'),
>        sql.identifier('p.direct_phone'),
>        sql.identifier('p.active'),
>        sql.identifier('c.org_name'),
>        sql.identifier('l.loc_nbr'),
>        sql.identifier('l.loc_name'),
>        sql.identifier('a.act_date'),
>        sql.identifier('a.act_type'),
>        sql.identifier('a.notes'),
>        sql.identifier('a.next_contact'),
>        ])
>        tables = sql.SQL(',').join([
>        sql.identifier('people as p'),
>        sql.identifier('companies as c on c.org_nbr = p.org_nbr'),
>        sql.identifier('locations as l on l.org_nbr = o.org_nbr and 
> l.loc_nbr = p.loc_nbr'),
>        sql.identifier('contacts as a on a.person_nbr = p.person_nbr'),
>        ])
>        where p.lname = (%s) and p.fname = (%s)
>        group by a.act_date order by a.act_date", (lname,fname))


This will not work, if for no other reason that 'companies as c on 
c.org_nbr = p.org_nbr', etc are not identifiers and it is sql.Identifier.

Also it would be simpler for this case just to create a quoted 
statement, something like:
"""SELECT
    fld_1, fld_2, fld_3
FROM
    some_table AS st
JOIN
    another_table AS a
ON
     st.id = a.a_id
WHERE
     st.fld_1 = 'something'
"""
> 
>> For an UPDATE example see my answer to this Stack Overflow question:
>>
https://stackoverflow.com/questions/68321746/how-to-create-a-dynamic-update-query-in-sql-using-python/68323019#comment120800261_68323019

>>
> 
> I'll read this again in close detail after I understand how to write insert
> and select queries.
> 
> Regards,
> 
> Rich
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com



psycopg by date:

Previous
From: Rich Shepard
Date:
Subject: Re: New user questions
Next
From: Adrian Klaver
Date:
Subject: Re: New user questions