On 05-11-2010 23:26, Tomeu Roig wrote:
>
> Hi, before to make the insert, you need change the string ascii to utf8,
>
> Newvalue=value.encode(utf-8)
>
first of all a small type, quotes around utf-8 needed.
Newvalue=value.encode( ' utf-8' )
But then it still doesn't work in my situation.
I think the problem is a bit more complex, sorry for the long story.
Normally I always use unicode strings in all my code, so there should never be
a problem ;-)
Using the DAL introduces a basic problem,
suppose I've a dictionair My_Dict that I want to insert in a DAL table
DB.Table.insert ( **My_Dict )
This is impossible, because the unpacking ** doesn't allow unicode keys.
In gluon.sql this way of unpacking is done frequently.
the next problem is that (because I generate my fieldnames by code), the
filednames and tablenames
becomes unicode:
Fields.append ( eval ( str ( FLine ) ) )
print 'LLVV', FLine, Fields[-1].name, type (Fields[-1].name)
resulting in UNICODE !!
LLVV Field ( 'Verzekering_ID','integer') Verzekering_ID <type 'unicode'>
the third problem is that in the Table._insert method
sql_f = ', '.join(fs)
sql_v = ', '.join(vs)
sql_t = self._tablename
print '======jdhsadjs======',type(sql_t), type(sql_f), type(sql_v)
return 'INSERT INTO %s(%s) VALUES (%s);' % (sql_t, sql_f, sql_v)
resulting in invalid combinations of unicode and strings
======jdhsadjs====== <type 'unicode'> <type 'unicode'> <type 'str'>
What really solves the problem is to convert sql_t and sql_f in Table._insert
into strings
sql_f = ', '.join(fs)
sql_v = ', '.join(vs)
sql_t = self._tablename
sql_t = sql_t.encode('utf-8')
sql_f = sql_f.encode('utf-8')
return 'INSERT INTO %s(%s) VALUES (%s);' % (sql_t, sql_f, sql_v)
I've the feeling that this is not a good solution in the first place.
Secondly, there probably are more of this code parts in gluon.sql.
Any suggestions ?
thanks,
Stef
>> El 05/11/2010 12:18, "Stef Mientki" <[email protected]
>> <mailto:[email protected]>>
>> escribió:
>>
>>
>> I'm trying to insert a record with a filed value
>> u"ëLocatie"
>>
>> and I get an error in sql.Table._insert at the last line:
>> return 'INSERT INTO %s(%s) VALUES (%s);' % (sql_t, sql_f, sql_v)
>>
>> here the traceback
>> File "D:\Data_Python_25\support\Web2Py_DAL_support.py", line 250, in
>> DAL_Table
>> Description = Value[3][:-1])
>> File "P:\Web2PY\web2py_src\web2py\gluon\sql.py", line 2035, in insert
>> query = self._insert(**fields)
>> File "P:\Web2PY\web2py_src\web2py\gluon\sql.py", line 2028, in _insert
>> return 'INSERT INTO %s(%s) VALUES (%s);' % (sql_t, sql_f, sql_v)
>> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 35:
>> ordinal not in range(128)
>>
>> I don't understand the problem and know what I'm doing wrong
>>
>> Wwith print statements I tried to narrow the problem, and in :
>> def sql_represent(obj, fieldtype, dbname, db_codec='UTF-8'):
>>
>> there's is this part
>> if isinstance(obj, unicode):
>> print '????', type(obj),obj
>> if len(obj)>0 : print ord(obj[0])
>> obj = obj.encode(db_codec)
>> print '????', type(obj)
>> if len(obj)>0 : print ord(obj[0])
>>
>> which seems to convert the unicode to a string with byte values larger than
>> 128.
>>
>> Am I doing something wrong, or is this a bug ?
>> And of course far more interesting, how do I solve this problem ?
>>
>> thanks,
>> Stef Mientki
>>
>>