Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #39661 > unrolled thread

Escaping list of numbers for Postgres column names

Started byandydtaylor@gmail.com
First post2013-02-23 03:55 -0800
Last post2013-02-23 10:40 -0800
Articles 2 — 1 participant

Back to article view | Back to comp.lang.python


Contents

  Escaping list of numbers for Postgres column names andydtaylor@gmail.com - 2013-02-23 03:55 -0800
    Re: Escaping list of numbers for Postgres column names andydtaylor@gmail.com - 2013-02-23 10:40 -0800

#39661 — Escaping list of numbers for Postgres column names

Fromandydtaylor@gmail.com
Date2013-02-23 03:55 -0800
SubjectEscaping list of numbers for Postgres column names
Message-ID<9f33141f-7cca-4f67-b030-bf16dda778fb@googlegroups.com>
Hi

I have some convenient short place name IDs which would be handy for column names. Unfortunately, many begin with a number. 

I can work around this by appending a letter to each one, but should I escape the number in such a way that I can use it directly as my column name, in the same way as I might an apostrophe? How could I do that? I wondered what the best approach is to take.


My python list come out looking  like this:

>>> print stn_list_short
['9400ZZLUACY1', '490000238006', '490000276005', '9400ZZLUACT2']

I'm producing it using this code:

>>> for row in cursor_from:
...     if row[8]:
...         # Station values for database
...         stn_list_short.append(row[0])

And trying to employ the list something like this:

>>> create_table_fields = '(id serial PRIMARY KEY, station_code varchar, %s varchar)' % ' varchar, '.join(map(str, "\" + str(stn_list_short)))


Thanks,


Andy

[toc] | [next] | [standalone]


#39696

Fromandydtaylor@gmail.com
Date2013-02-23 10:40 -0800
Message-ID<bda02f99-ba2c-4b37-8953-bef1db336650@googlegroups.com>
In reply to#39661
In reply to my own question, postgres column names must begin with a letter or an underscore. So this is what I have done:

>>> for row in cursor_from:
...     if row[8]:
...         stn_list_short.append("_" + row[0])

I can now use stn_list_short to create my columns

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web