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]))
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))
> 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