Problems Writing £ (pound sterling) To MS SQL Server using pymssql

Tim Golden mail at timgolden.me.uk
Mon Nov 17 10:24:44 EST 2008


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).


This is a bit of a non-answer but... use pyodbc[*],
use NVARCHAR cols, and use unicode values on insert:

<code>
import pyodbc
import unicodedata

db = pyodbc.connect (r"Driver={SQL Server};Server=SVR17;Database=TDI;TrustedConnection=Yes")

db.execute ("CREATE TABLE b (x NVARCHAR (1))")
db.execute ("INSERT INTO b (x) VALUES (?)", ['£'])
db.execute ("INSERT INTO b (x) VALUES (?)", [u'£'])
db.execute ("COMMIT")

for x, in db.execute ("SELECT x FROM b"):
  print repr (x), unicodedata.name (x[0])

</code>


[*] Don't know if it's strictly necessary, but I gave up
on pymssql a while back when it had issues with unicode and
was using the ntwdblib which is more-and-more unsupported.

TJG



More information about the Python-list mailing list