Problems Writing £ (pound sterling) To MS SQLServer using pymssql

Darren Mansell darren.mansell at opengi.co.uk
Thu Nov 27 11:48:43 CET 2008


On Mon, 2008-11-17 at 14:52 +0000, Darren Mansell wrote:
> Hi. 
> 
> I'm relatively new to python so please be gentle :)
> 
> I'm trying to write a £ symbol to an MS SQL server using pymsssql . This
> works but when selecting the data back (e.g. using SQL management
> studio) the £ symbol is replaced with £ (latin capital letter A with
> circumflex).
> 
> I can reproduce it like so:
> 
> >>> con = pymssql.connect(host='testdb',user='testuser',password='password',database='test')
> >>> sql = "insert into table_1 values ('£')"
> >>> cur.execute(sql)
> >>> con.commit()
> >>> sql = "insert into table_1 values ('1')"
> >>> cur.execute(sql)
> >>> con.commit()
> >>> sql = "select * from table_1"
> >>> cur.execute(sql)
> >>> cur.fetchall()
> [('\xc2\xa3',), ('1',)]
> 
> If I insert a £ symbol in using SQL management studio then select it back from Python I get this:
> 
> [('\xc2\xa3',), ('1',), ('\xa3',)]
> 
> If I look in SQL management studio it says this:
> 
> £ 
> 
> for the inserts I do through Python/pymssql.
> 
> Does anyone have any ideas whats happening and how to fix it?
> 
> Thanks

Thank you to everyone who replied to this and gave me pointers.

In the end this post
http://mail.python.org/pipermail/python-list/2006-August/397131.html
gave me the answer I needed. I had to encode to code page 1252 to use
unicode in normal VARCHAR columns.

Cheers
Darren



More information about the Python-list mailing list