Then it's not coming out of the DB as utf-8. You'll need to find out
what encoding it is and use that one to convert to unicode.
I´m so sorry Robin, I'm still in trouble, but, I am trying harder to
resolve it. In an earlier email, I wrote that I created a new test
base with an only field to look step by step what was happening. Ok,
playing wityh it, I saw an interesting thing. IMHO the problem is
apply when the data is SENT to the db, no when it is retrieved. I'll
try to explain:
I sent a string through the wx.TestApp like 'Pipí'. When I saw it in
the MySQL console:
···
On 09/06/07, Robin Dunn <robin@alldunn.com> wrote:
select test from prueba;
+--------+
test |
+--------+
Pipí |
+--------+
(or 'Pipí' in phpmyadmin)
Ok?, but when I try to retrieve it I saw in my python console: (I'll
repeat my code below)
Before to send: Pipí
Tuple result of SQL (('Pip\xed',),)
First field of tuple: Pip
Type : <type 'str'>
Traceback (most recent call last):
File "test.py", line 39, in OnInsert
uniValue = value.decode('utf-8')
File "encodings/utf_8.py", line 16, in decode
UnicodeDecodeError: 'utf8' codec can't decode byte 0xed in position 3:
unexpected end of data
OK. Only a few more seconds please...
If I make the same stuff with PyCrust:
c = MySQLdb.connect('localhost', 'javier', 'javier', 'prueba')
cur = c.cursor()
cur.execute('''INSERT INTO prueba (test) VALUES (%s)''', 'Pipí')
1L
and I saw in my MySQL console:
select test from prueba;
+---------+
test |
+---------+
PipÂ- |
+---------+
(Or 'PipÃ' in phpmyadmin)
But when I retrieve it, it looks smart and brigthing:
cur.execute('''SELECT test FROM prueba''')
1L
d = cur.fetchall()
print d
(('Pip\xc3\xad',),)
print d[0][0]
Pipí
IMHO, my data is looking so different just when I store in my db. The
settings in my db are:
show variables like "%character%";
+--------------------------+----------------------------+
Variable_name | Value |
+--------------------------+----------------------------+
character_set_client | utf8 |
character_set_connection | utf8 |
character_set_database | utf8 |
character_set_filesystem | binary |
character_set_results | utf8 |
character_set_server | utf8 |
character_set_system | utf8 |
character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
Is that email a help?? I hope so.
BTW, the entire project is a school administrator for popular schools.
I coordinate a poor school where I work since 1993. Whe offers PC
repair, electrician, mechanic and PC operator curses (officially
certified)with no cost for the pupils. It's a hard work, but we are
very proud to work in that place. Every single cent that is necessary
to open the school, may be obtained by us. The state doesn't apports
anything, only the certification. Now I want to deploy an
administrator (this project) cause the administrative stuff is hard
and heavy, and till now, all is made with paper and pencil. Obviously,
it will be GPL to offers it to similar colleges along the contry. If
you want, you can visit my school at
http://www.sancayetano.esc.edu.ar. All PC curses uses only and
exclusively SL. We are pioneers in that in our zone.
Thanks a lot to everybody, regards from Provincia de Buenos Aires, Argentina.
The code of my wx.TestApp is:
import wx
import sys
import MySQLdb
class App(wx.App):
def OnInit(self):
frame = Frame('Prueba Unicode', (30, 30), (300, 300))
frame.Show()
self.SetTopWindow(frame)
return True
class Frame(wx.Frame):
def __init__(self, title, pos, size):
wx.Frame.__init__(self, None, -1, title, pos, size)
self.db = MySQLdb.connect("localhost", "javier", "javier", "prueba")
panel = wx.Panel(self, -1)
text = wx.StaticText(panel, -1, 'Data to insert: ', (40, 40))
self.datatodb = wx.TextCtrl(panel, -1, '', (150, 30), (120, -1))
self.datatodb.SetFocus()
BtnInsert = wx.Button(panel, -1, 'Insert',(120, 150))
self.Bind(wx.EVT_BUTTON, self.OnInsert, BtnInsert)
def OnInsert(self, event):
datain = self.datatodb.GetValue()
print 'Before to send: ', datain
c = self.db.cursor()
c.execute('''INSERT INTO prueba(test) VALUES (%s)''', (datain))
self.datatodb.SetValue('')
c.execute ('''SELECT test from prueba WHERE test = %s''', (datain))
d=c.fetchall()
print 'Tuple result of SQL', d
value = d[0][0]
print 'First field of tuple: ', value
tipo = type(value)
print 'Type : ', tipo
uniValue = value.decode('utf-8')
print 'string.decode: ', uniValue
print
#Loop principal
if __name__== '__main__':
app = App()
app.MainLoop()
--
Javier Castrillo
=========================================================
GNU / Linux User #242275
-------------------------------
pub 1024D/B482896F 2006-09-04
uid Javier Castrillo (El Palo) <riverplatense@gmail.com>
sub 2048g/52C99A4E 2006-09-04
Clave pública: carapa.com.ar/public.asc
http://carapa.com.ar
http://javiercastrillo.com.ar
http://riverplatense.googlepages.com
Usá Software Libre