User input to queries

Started by Rich Shepardover 5 years ago15 messagesgeneral
Jump to latest
#1Rich Shepard
rshepard@appl-ecosys.com

While I develop the application's GUI I use the database from the command
line (psql). While some queries are self-contained others need user input.
I've not found a search term that locates this information in the
postgres-12 user manual and my web searches suggest that using '&' should
work.

I tried the latter with this query but postgres didn't like it. (There may
well be other errors in it so don't hesitate to point out my mistakes.)

------
/* This query selects all activity information for a named person */

/* Specify columns */
SELECT p.lname, p.fname, p.loc_nbr, p.job_title, p.direct_phone, p.active,
o.org_name,
l.loc_nbr, l.loc_name,
a.act_date, a.act_type, a.notes, a.next_contact
/* Specify tables. */
FROM People AS p
JOIN Organizations AS o ON o.org_nbr = p.org_nbr
JOIN Locations AS l ON l.org_nbr = o.org_nbr and l.loc_nbr = p.loc_nbr
JOIN Activities AS a ON a.person_nbr = p.person_nbr
/* Specify rows */
WHERE p.lname = &p.lname AND p.fname = &p.fname;
-----

Suggestions on what I should read to learn more about this subject are
appreciated.

Regards,

Rich

#2Michael Lewis
mlewis@entrata.com
In reply to: Rich Shepard (#1)
Re: User input to queries

On Wed, Dec 9, 2020 at 10:04 AM Rich Shepard <rshepard@appl-ecosys.com>
wrote:

While I develop the application's GUI I use the database from the command
line (psql). While some queries are self-contained others need user input.
I've not found a search term that locates this information in the
postgres-12 user manual and my web searches suggest that using '&' should
work.

I tried the latter with this query but postgres didn't like it. (There may
well be other errors in it so don't hesitate to point out my mistakes.)

------
/* This query selects all activity information for a named person */

/* Specify columns */
SELECT p.lname, p.fname, p.loc_nbr, p.job_title, p.direct_phone, p.active,
o.org_name,
l.loc_nbr, l.loc_name,
a.act_date, a.act_type, a.notes, a.next_contact
/* Specify tables. */
FROM People AS p
JOIN Organizations AS o ON o.org_nbr = p.org_nbr
JOIN Locations AS l ON l.org_nbr = o.org_nbr and l.loc_nbr =
p.loc_nbr
JOIN Activities AS a ON a.person_nbr = p.person_nbr
/* Specify rows */
WHERE p.lname = &p.lname AND p.fname = &p.fname;
-----

Suggestions on what I should read to learn more about this subject are
appreciated.

Regards,

Rich

Are you looking for this perhaps?

https://www.postgresql.org/docs/current/sql-prepare.html

#3Rich Shepard
rshepard@appl-ecosys.com
In reply to: Michael Lewis (#2)
Re: User input to queries

On Wed, 9 Dec 2020, Michael Lewis wrote:

Are you looking for this perhaps?
https://www.postgresql.org/docs/current/sql-prepare.html

Michael,

I don't think so. Reading the PREPARE doc page my understanding is that its
use is for statement execution optimization, not asking for user data input
for variables in the query statement. It's useful information, though not
for my immediate need.

Thanks and stay well,

Rich

#4Michael Lewis
mlewis@entrata.com
In reply to: Rich Shepard (#3)
Re: User input to queries

What application is taking the user input and needs to include the
parameters in the query string?

#5Rob Sargent
robjsargent@gmail.com
In reply to: Rich Shepard (#1)
Re: User input to queries

On 12/9/20 10:03 AM, Rich Shepard wrote:

While I develop the application's GUI I use the database from the command
line (psql). While some queries are self-contained others need user
input.
I've not found a search term that locates this information in the
postgres-12 user manual and my web searches suggest that using '&' should
work.

I tried the latter with this query but postgres didn't like it. (There
may
well be other errors in it so don't hesitate to point out my mistakes.)

------
/* This query selects all activity information for a named person */

/* Specify columns */
SELECT p.lname, p.fname, p.loc_nbr, p.job_title, p.direct_phone,
p.active,
       o.org_name,
       l.loc_nbr, l.loc_name,
       a.act_date, a.act_type, a.notes, a.next_contact
/* Specify tables. */ FROM People AS p
     JOIN Organizations AS o ON o.org_nbr = p.org_nbr
     JOIN Locations AS l ON l.org_nbr = o.org_nbr and l.loc_nbr =
p.loc_nbr
     JOIN Activities AS a ON a.person_nbr = p.person_nbr
/* Specify rows */ WHERE p.lname = &p.lname AND p.fname = &p.fname;
-----

Suggestions on what I should read to learn more about this subject are
appreciated.

Regards,

Rich

Put the query in a file, set the desired name, then from psql

\i filename

Edit pfname, repeat

#6Laurenz Albe
laurenz.albe@cybertec.at
In reply to: Rich Shepard (#1)
Re: User input to queries

On Wed, 2020-12-09 at 09:03 -0800, Rich Shepard wrote:

While I develop the application's GUI I use the database from the command
line (psql). While some queries are self-contained others need user input.
I've not found a search term that locates this information in the
postgres-12 user manual and my web searches suggest that using '&' should
work.

I tried the latter with this query but postgres didn't like it. (There may
well be other errors in it so don't hesitate to point out my mistakes.)

------
/* This query selects all activity information for a named person */

/* Specify columns */
SELECT p.lname, p.fname, p.loc_nbr, p.job_title, p.direct_phone, p.active,
o.org_name,
l.loc_nbr, l.loc_name,
a.act_date, a.act_type, a.notes, a.next_contact
/* Specify tables. */
FROM People AS p
JOIN Organizations AS o ON o.org_nbr = p.org_nbr
JOIN Locations AS l ON l.org_nbr = o.org_nbr and l.loc_nbr = p.loc_nbr
JOIN Activities AS a ON a.person_nbr = p.person_nbr
/* Specify rows */
WHERE p.lname = &p.lname AND p.fname = &p.fname;
-----

Suggestions on what I should read to learn more about this subject are
appreciated.

You probably need the \prompt psql command:

\prompt 'What is "p.lname"' p_lname
\prompt 'What is "p.fname"' p_fname
SELECT ... WHERE p.lname = :p_lname AND p.fname = :p_fname;

Yours,
Laurenz Albe
--
Cybertec | https://www.cybertec-postgresql.com

#7Rich Shepard
rshepard@appl-ecosys.com
In reply to: Michael Lewis (#4)
Re: User input to queries

On Wed, 9 Dec 2020, Michael Lewis wrote:

What application is taking the user input and needs to include the
parameters in the query string?

Michael,

My business tracking tool. Yes, the GUI will have text entry widgets for
user input but I want to apply these queries using psql on the command line
until I build the GUI.

Rich

#8Rich Shepard
rshepard@appl-ecosys.com
In reply to: Laurenz Albe (#6)
Re: User input to queries [RESOLVED]

On Wed, 9 Dec 2020, Laurenz Albe wrote:

You probably need the \prompt psql command:
\prompt 'What is "p.lname"' p_lname
\prompt 'What is "p.fname"' p_fname
SELECT ... WHERE p.lname = :p_lname AND p.fname = :p_fname;

Laurenz,

Ah! I have not before encountered that command. Yes, this will do the job
and I'll make it a point to look at _all_ available psql commands.

Thanks very much.

Stay well,

Rich

#9Rich Shepard
rshepard@appl-ecosys.com
In reply to: Rob Sargent (#5)
Re: User input to queries

On Wed, 9 Dec 2020, Rob Sargent wrote:

Put the query in a file, set the desired name, then from psql
\i filename
Edit pfname, repeat

Thanks, Rob.

Stay well,

Rich

#10Paul Förster
paul.foerster@gmail.com
In reply to: Rich Shepard (#7)
Re: User input to queries

Hi Rich,

On 09. Dec, 2020, at 18:53, Rich Shepard <rshepard@appl-ecosys.com> wrote:

My business tracking tool. Yes, the GUI will have text entry widgets for
user input but I want to apply these queries using psql on the command line
until I build the GUI.

maybe you're looking for this?

https://stackoverflow.com/a/7389606

Cheers,
Paul

#11Rich Shepard
rshepard@appl-ecosys.com
In reply to: Paul Förster (#10)
Re: User input to queries

On Wed, 9 Dec 2020, Paul Fᅵrster wrote:

maybe you're looking for this?
https://stackoverflow.com/a/7389606

Paul,

That looks very useful and I'll try the provided answers.

Looking at the postgres-12 doc I cannot find a command 'PROMPT' anywhere in
it.

Thanks,

Rich

#12Rob Sargent
robjsargent@gmail.com
In reply to: Rich Shepard (#11)
Re: User input to queries

On 12/9/20 11:10 AM, Rich Shepard wrote:

On Wed, 9 Dec 2020, Paul Fᅵrster wrote:

maybe you're looking for this?
https://stackoverflow.com/a/7389606

Paul,

That looks very useful and I'll try the provided answers.

Looking at the postgres-12 doc I cannot find a command 'PROMPT'
anywhere in
it.

Thanks,

Rich

https://www.postgresql.org/docs/12/app-psql.html

#13Paul Förster
paul.foerster@gmail.com
In reply to: Rich Shepard (#11)
Re: User input to queries

Hi Rich,

On 09. Dec, 2020, at 19:10, Rich Shepard <rshepard@appl-ecosys.com> wrote:

Looking at the postgres-12 doc I cannot find a command 'PROMPT' anywhere in
it.

but 12 has it:

postgres=# \prompt 'input: ' input
input: this is test input
postgres=# select version(), :'input';
version | ?column?
------------------------------------------------------------------------------------+--------------------
PostgreSQL 12.5 on x86_64-pc-linux-gnu, compiled by gcc (SUSE Linux) 7.5.0, 64-bit | this is test input
(1 row)

postgres=# \?
...
Variables
\prompt [TEXT] NAME prompt user to set internal variable
\set [NAME [VALUE]] set internal variable, or list all if no parameters
\unset NAME unset (delete) internal variable
...

Cheers,
Paul

#14Rich Shepard
rshepard@appl-ecosys.com
In reply to: Paul Förster (#13)
Re: User input to queries

On Wed, 9 Dec 2020, Paul Fᅵrster wrote:

but 12 has it:

postgres=# \prompt 'input: ' input
input: this is test input
postgres=# select version(), :'input';

Paul,

Okay. I use mupdf to view the document and my search string were 'prompt',
and 'prompt command'. I didn't use '\prompt',

Thanks again,

Rich

#15Paul Förster
paul.foerster@gmail.com
In reply to: Rich Shepard (#14)
Re: User input to queries

Hi Rich,

On 09. Dec, 2020, at 19:22, Rich Shepard <rshepard@appl-ecosys.com> wrote:

Okay. I use mupdf to view the document and my search string were 'prompt',
and 'prompt command'. I didn't use '\prompt',

\prompt is a psql special command, hence the backslash. Only psql knows that, the database does not, as with all backslash commands.

Cheers,
Paul