Re: [sqlite] Re: Python-Sqlite Unicode characters

2007-03-30 Thread Kostas5904


This is how I solved the problem (in case somebody else face it in the
future): 
...
item=dialog.GetValue()
cols=id,attr,type
table=names1
v=(%s, '%s', %s) % (null,item,3)  #the whole job is done by the ' 
%s'
a=insert into %s (%s) values %s % (table,cols,v)

and then...

cursor.execute (a)

ATTENTION: If you work in Python you have to encode the value to utf-8, like
this:

item=dialog.GetValue().encode('utf-8')

otherwise, if you want to print a,  you will get the following error (for
the unicode characters) :

UnicodeEncodeError: 'ascii' codec can't encode characters in position 49-56:
ordinal not in range(128)

One more time, thank you very much for your help!

Kostas

P.S. Igor you are right for the security. I am working on it...



-- 
View this message in context: 
http://www.nabble.com/Python-Sqlite-Unicode-characters-tf3486826.html#a9749435
Sent from the SQLite mailing list archive at Nabble.com.


-
To unsubscribe, send email to [EMAIL PROTECTED]
-



[sqlite] Python-Sqlite Unicode characters

2007-03-29 Thread Kostas5904

Hi, everybody. I am new in Python and Sqlite and I have a problem when I'm
using unicode strings. Actually I'm working with WxPython and pysqlite (to
connect to sqlite).

The problem is as follows:

When I execute this: 

cursor.execute('INSERT INTO names1 (id,decr,type)
VALUES (null,Βασίλης,7)') 
   
everything works fine. 

The values are stored in the table and when I retrieve them (select * from
names1...), everything is OK. (If you cannot read the literal value: is in
Greek). The values are shown in a wxpython list control perfect.

But...

When I ask a value from the user and I try to store it into the same table,
I run the following code
 
 item=dialog.GetValue()
 table=names1
 cols=id,descr,type
 values=(null,item,3)
 a=insert into %s (%s) values %s % (table,cols,values)
 cursor.execute(a)

and I get the error:

pysqlite2.dbapi2.OperationalError: near
'\u03b1\u03bd\u03c4\u03ce\u03bd\u03b7\u03c2': syntax error

I do understand that this one happens because item=
u'\u03b1\u03bd\u03c4\u03ce\u03bd\u03b7\u03c2'. 

But how can I go trhought this???

I tried to use:  values=(null,item.encode('utf-8'),3). The result is that
I store in the database the string without u in front of the string
('\u03b1\u03bd\u03c4\u03ce\u03bd\u03b7\u03c2') but...

When I execute Select * from names1... then I get this as the string I
stored:

  '\u03b1\u03bd\u03c4\u03ce\u03bd\u03b7\u03c2' 

and not the equivalent unicode string.

Can anybody help?








-- 
View this message in context: 
http://www.nabble.com/Python-Sqlite-Unicode-characters-tf3486826.html#a9735074
Sent from the SQLite mailing list archive at Nabble.com.


-
To unsubscribe, send email to [EMAIL PROTECTED]
-