Обсуждение: char(8) vs char8

Поиск
Список
Период
Сортировка

char(8) vs char8

От
darcy@druid.net (D'Arcy J.M. Cain)
Дата:
This was an unexpected difference between these two types and I wonder
if it was meant to be this way.  Previously, a char8 field with the
string 'abc' would return 'abc' as expected.  Now, with char(8), I get
back 'abc     ' instead.  You can see this with my PygreSQL module
or the C interface (which my module uses, of course.)  This causes a
lot of my programs to break.

I have made a quick change to my Python module to handle this.  Should
I clean it up or can I expect the behaviour to go back the way it was?

--
D'Arcy J.M. Cain <darcy@{druid|vex}.net>   |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 424 2871     (DoD#0082)    (eNTP)   |  what's for dinner.

Re: [HACKERS] char(8) vs char8

От
Byron Nikolaidis
Дата:

D'Arcy J.M. Cain wrote:

> This was an unexpected difference between these two types and I wonder
> if it was meant to be this way.  Previously, a char8 field with the
> string 'abc' would return 'abc' as expected.  Now, with char(8), I get
> back 'abc     ' instead.  You can see this with my PygreSQL module
> or the C interface (which my module uses, of course.)  This causes a
> lot of my programs to break.
>

char(x) is the datatype 'bpchar' (blank padded char).  Thus it is padded
with spaces to the field width.

Couldn't you use something like "select rtrim(column) from table".  This
will trim the spaces off.

Byron


Re: [HACKERS] char(8) vs char8

От
"Thomas G. Lockhart"
Дата:
> This was an unexpected difference between these two types and I wonder
> if it was meant to be this way.  Previously, a char8 field with the
> string 'abc' would return 'abc' as expected.  Now, with char(8), I get
> back 'abc     ' instead.  You can see this with my PygreSQL module
> or the C interface (which my module uses, of course.)  This causes a
> lot of my programs to break.
>
> I have made a quick change to my Python module to handle this.  Should
> I clean it up or can I expect the behaviour to go back the way it was?

The behavior you want is varchar() rather than char().

                    - Tom

Re: [HACKERS] char(8) vs char8

От
darcy@druid.net (D'Arcy J.M. Cain)
Дата:
Thus spake Byron Nikolaidis
> > string 'abc' would return 'abc' as expected.  Now, with char(8), I get
> > back 'abc     ' instead.  You can see this with my PygreSQL module
>
> Couldn't you use something like "select rtrim(column) from table".  This
> will trim the spaces off.

It wouldn't be as convenient as this example.

import pg
for d in pg.connect('database').query('select * from table').dictresult():
    print "Num: %(field1)3d, '%(field2)s'" % d

--
D'Arcy J.M. Cain <darcy@{druid|vex}.net>   |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 424 2871     (DoD#0082)    (eNTP)   |  what's for dinner.

Re: [HACKERS] char(8) vs char8

От
darcy@druid.net (D'Arcy J.M. Cain)
Дата:
Thus spake Thomas G. Lockhart
> > string 'abc' would return 'abc' as expected.  Now, with char(8), I get
> > back 'abc     ' instead.  You can see this with my PygreSQL module
> The behavior you want is varchar() rather than char().

Right.  Dump and reload time I guess.  >:-/

--
D'Arcy J.M. Cain <darcy@{druid|vex}.net>   |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 424 2871     (DoD#0082)    (eNTP)   |  what's for dinner.