Re: Encountered an error

Поиск
Список
Период
Сортировка
От Adrian Klaver
Тема Re: Encountered an error
Дата
Msg-id 56E1DDD8.6060809@aklaver.com
обсуждение исходный текст
Ответ на Re: Encountered an error  (Shaan Repswal <shaun.reapswaal@gmail.com>)
Список psycopg
On 03/10/2016 11:46 AM, Shaan Repswal wrote:
> I am new to databases and am a novice to programming in general. But I
> have a problem and this is the only way it seems to me it will work. I
> plan on learning the rest later. After I've created this program. To
> give you guys an idea of how naive I am. Please know that a lot of this
> just went over my head.
>
> @Daniele if I can't use %s what can I use? The "%l" that Adrian suggested?

They come from different systems, the %s from Python string formatting,
the %I from Postgres formatting, so they cannot be mixed. This is not
strictly true as Postgres also uses %s. See below for respective docs(I
am using Python 2 docs):

Python
https://docs.python.org/2/library/stdtypes.html#string-formatting

Postgres
http://www.postgresql.org/docs/9.5/interactive/functions-string.html#FUNCTIONS-STRING-FORMAT

> @Daniele What do you mean escaping values into identifier names?

See here:

http://initd.org/psycopg/docs/usage.html#passing-parameters-to-sql-queries

>
> @Adrian - I am Python string formatting? Whatt kind of string
> formatting. Which query should I format? The "Add a new column" query or
> the "add a value to the column of the currently entered record" query
> located right after it.

See the above Python string formatting section on how to build a string.
Build it and then pass that to your cursor to execute.

> @Adrian - I don't think I understand what you mean by the example of the
> format() function. Am I supposed to use it in psql? But my program is in
> Python... Is there some manual where I could get more info on this and
> where I could see a few examples of this function?

No I was showing an example in psql as it was quick and easy. You can
just build the string something like this(not tested)

cur.execute("select format('ALTER tbl_a ADD %I int DEFAULT NULL', 'fld_2')")

query_str = cur.fetchone()[0]

query_str should be the built string that you could then execute:

cur.execute(query_str)

For examples see the Postgres link above.

Also a previous thread on this list, starting here:

http://www.postgresql.org/message-id/554B69A9.2070209@aklaver.com

>
> @Karsten - I know, I got the feeling that the design felt awry as well.
> The database will be on the local machine. And the client side will have
> the code to error check the column names being added and also if there
> already is a column by this name or not.
>
>
>
> On Thu, Mar 10, 2016 at 8:02 PM, Karsten Hilbert
> <Karsten.Hilbert@gmx.net <mailto:Karsten.Hilbert@gmx.net>> wrote:
>
>     On Thu, Mar 10, 2016 at 03:30:14PM +0100, Karsten Hilbert wrote:
>
>     > While this is a nice solution to OPs problem I cannot resist
>     > the urge to point out that to me the whole approach of adding
>     > a column from within client code (outside a database
>     > management application) does have at least a whiff of design
>     > smell to it.
>
>     Like, what happens if the user enters something to be used as
>     a column name which isn't suitable for an identifier ?
>
>     And then what if two different unsuitable user input values
>     get normalized into the same column-name-suitable version ?
>
>     Karsten
>     --
>     GPG key ID E4071346 @ eu.pool.sks-keyservers.net
>     <http://eu.pool.sks-keyservers.net>
>     E167 67FD A291 2BEA 73BD  4537 78B9 A9F9 E407 1346
>
>
>     --
>     Sent via psycopg mailing list (psycopg@postgresql.org
>     <mailto:psycopg@postgresql.org>)
>     To make changes to your subscription:
>     http://www.postgresql.org/mailpref/psycopg
>
>


--
Adrian Klaver
adrian.klaver@aklaver.com


В списке psycopg по дате отправления:

Предыдущее
От: Shaan Repswal
Дата:
Сообщение: Re: Encountered an error
Следующее
От: Karsten Hilbert
Дата:
Сообщение: Re: Encountered an error