public inbox for [email protected]  
help / color / mirror / Atom feed
From: Adrian Klaver <[email protected]>
To: Rich Shepard <[email protected]>
To: [email protected]
Subject: Re: New user questions
Date: Wed, 14 Jul 2021 10:57:47 -0700
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
References: <[email protected]>
	<[email protected]>
	<[email protected]>
	<[email protected]>
	<[email protected]>

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-pytho... 
>>
> 
> I'll read this again in close detail after I understand how to write insert
> and select queries.
> 
> Regards,
> 
> Rich
> 
> 


-- 
Adrian Klaver
[email protected]





view thread (10+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected]
  Subject: Re: New user questions
  In-Reply-To: <[email protected]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox