Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whitespace in returned query with Python and py-postgresql

I'm using the py-postgresql driver. Whenever I do a SELECT it works correctly, but I get the full width of the column returned, with whitespace. So if I have a field of character(25) which holds "Name", and do:

SELECT name WHERE name = 'Name'

I get

"Name                     " << "Name" followed by 21 spaces
instead of just Name

Example Code that I've run straight from IDLE

db = posgresql.driver.connect(user     = [USERNAME],
                              password = [PASSWORD],
                              host     = [HOST],
                              port     = [PORT]
)

db.query("SELECT [FIELD] FROM [TABLE] WHERE [TRUE]")

No issues with it returning, and yes, I know how to strip the whitespace off the end, but it may need to be there, so I'm really trying to figure out how to get things to return without excess whitespace.

like image 226
David Avatar asked Dec 12 '14 10:12

David


1 Answers

Your column is probably defined as a CHAR(n) type, and the database has to honor that, so it is adding spaces at the end. You should switch your column type to VARCHAR(n), which is a variable length field with maximum length of n, and there will be no more trailing spaces.

like image 69
bosnjak Avatar answered Nov 18 '22 19:11

bosnjak