Shantanu's Blog

Corporate Consultant

December 17, 2014

 

Working with unicode strings in mysql

The unicode characters are not allowed to be stored in latin1
You will get an error as shown below:

drop table todel;
create table todel (name varchar(100)) DEFAULT CHARSET=latin1;

insert into todel values ('हिदी' )
Error in query (1366): Incorrect string value: '\xE0\xA4\xB9\xE0\xA4\xBF...' for column 'name' at row 1

insert into todel values (convert ('हिदी' using binary));
select convert(convert(name using binary) using utf8) from todel;

The work-around is to store the record as binary and while selecting, use convert function twice as shown above.
_____

There is a better way though. Why not to use utf8 encoding for the entire table?

drop table todel;
create table todel (name varchar(100)) DEFAULT CHARSET=utf8;

insert into todel values ('हिदी' );
select * from todel;
_____

You think that altering the table to utf8 will solve this issue?

alter table todel default charset=utf8;

No. Because even if the default table type is now utf8, the columns are still latin1

 mysql> show create table todel;
+-------+---------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                        |
+-------+---------------------------------------------------------------------------------------------------------------------+
| todel | CREATE TABLE `todel` (
  `name` varchar(100) CHARACTER SET latin1 DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+---------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

alter table todel modify name varchar(100) character set utf8 ;

Now even if we have column as well as table type utf8, we still get junk characters instead of unicode.

mysql> select * from todel;
+---------------------------+
| name                      |
+---------------------------+
| हिदी              |
+---------------------------+
1 row in set (0.00 sec)

The bad news is that the convert query that was working fine earlier has now stopped working as expected.

mysql> select convert(convert(name using binary) using utf8) from todel;
+------------------------------------------------+
| convert(convert(name using binary) using utf8) |
+------------------------------------------------+
| हिदी                                   |
+------------------------------------------------+
1 row in set (0.00 sec)

The unicode data is lost in the conversion.
_____

So the correct solution would be to add a utf8 column to latin1 table and update that column data with correct unicode string.
Let's start all over again:

drop table todel;
create table todel (name varchar(100)) DEFAULT CHARSET=latin1;
insert into todel values (convert ('हिदी' using binary));
select convert(convert(name using binary) using utf8) from todel;

alter table todel add column hindi varchar(100) character set utf8;

update todel set hindi = convert(convert(name using binary) using utf8) ;

mysql> select convert(convert(name using binary) using utf8), hindi from todel;
+------------------------------------------------+--------------+
| convert(convert(name using binary) using utf8) | hindi        |
+------------------------------------------------+--------------+
| हिदी                                           | हिदी         |
+------------------------------------------------+--------------+
1 row in set (0.00 sec)

_____

Now the problem is that the application is not aware of this new column called "hindi" and it is still using column "name". We need an insert and update trigger to keep correcting the values in the hindi column. Something like this...

delimiter |
CREATE TRIGGER todel_bi after INSERT ON todel
  FOR EACH ROW
  BEGIN
    UPDATE todel SET hindi = convert(convert(name using binary) using utf8) WHERE id = NEW.id;
  END;
|

insert into todel (id, name) values (2, (convert ('मराठी' using binary)));

I thought the after insert trigger would solve this issue, but I get an error.

ERROR 1442 (HY000): Can't update table in trigger because it is already used by statement which invoked this stored function/trigger.

Now I need to write a stored procedure that inserts the record in the target table and change the code accordingly (that will call the procedure).
_____

So we are back where we started. Why not simply replace the name column with this new unicode aware column?

drop table todel;
create table todel (name varchar(100)) DEFAULT CHARSET=latin1;
insert into todel values (convert ('हिदी' using binary));

alter table todel add column hindi varchar(100) character set utf8;
update todel set hindi = convert(convert(name using binary) using utf8) ;
alter table todel drop column name;
alter table todel change column hindi name varchar(100) character set utf8;

Labels: ,


November 27, 2014

 

Python tutorial

# halve the numbers if the number in the list is divisible by 2

myl = [3, 4, -5, 8, 18, -3]
for i, num in enumerate(myl):
    if num % 2 == 0:
        myl[i] = num // 2
myl

[3, 2, -5, 4, 9, -3]

# halve every alternate number

myl = [3, 4, -5, 8, 18, -3]
for i, num in enumerate(myl):
    if i % 2 == 0:
        myl[i] = num // 2
myl

[1, 4, -3, 8, 9, -3]

Labels:


November 14, 2014

 

unbound methods in python


class Base2(object):
    def greet_visitor(self, name): print "welcome ", name
class Sub(Base2):
    def greet(self, name):
        print "well Met and",
        Base2.greet_visitor(self, name)
x=Sub()
x.greet('programmer')

The delegation to the superclass, in the body of Sub.greet, uses an unbound method obtained by attribute reference Base.greet_visitor on the superclass, and therefore passes all attributes normally, including self. Delegating to a superclass implementation is the most frequent use of unbound methods.

Labels:


November 13, 2014

 

Get your gmail

You can get the list of gmail mailboxes using the following 4 or 5 lines of code.

import imaplib
M = imaplib.IMAP4_SSL('imap.gmail.com')
M.login('your.name@gmail.com', 'passwd')

rv, mailboxes = M.list()
   
import pprint
pprint.pprint(mailboxes)

You may need to disable secure access from gmail setting:
https://www.google.com/settings/security/lesssecureapps

Labels:


October 27, 2014

 

Generate series numbers

Here is the Mysql code that can be used to quickly generate 5 digit numbers starting from 10000 to 99999

drop table t;
create table t (series int);

set @n = 1;
drop view if exists v3;
drop view if exists v10;
drop view if exists v100;
create view v3 as select null union all select null union all select null;
create view v10 as select null from v3 a, v3 b union all select null;
create view v100 as select null from v10 a, v10 b;
insert into t select @n:=@n+1 from v10 a,v100 b, v100 c;
delete from  t where length(series) != 5;

Labels:


Archives

June 2001   July 2001   January 2003   May 2003   September 2003   October 2003   December 2003   January 2004   February 2004   March 2004   April 2004   May 2004   June 2004   July 2004   August 2004   September 2004   October 2004   November 2004   December 2004   January 2005   February 2005   March 2005   April 2005   May 2005   June 2005   July 2005   August 2005   September 2005   October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006   January 2007   February 2007   March 2007   April 2007   June 2007   July 2007   August 2007   September 2007   October 2007   November 2007   December 2007   January 2008   February 2008   March 2008   April 2008   July 2008   August 2008   September 2008   October 2008   November 2008   December 2008   January 2009   February 2009   March 2009   April 2009   May 2009   June 2009   July 2009   August 2009   September 2009   October 2009   November 2009   December 2009   January 2010   February 2010   March 2010   April 2010   May 2010   June 2010   July 2010   August 2010   September 2010   October 2010   November 2010   December 2010   January 2011   February 2011   March 2011   April 2011   May 2011   June 2011   July 2011   August 2011   September 2011   October 2011   November 2011   December 2011   January 2012   February 2012   March 2012   April 2012   May 2012   June 2012   July 2012   August 2012   October 2012   November 2012   December 2012   January 2013   February 2013   March 2013   April 2013   May 2013   June 2013   July 2013   September 2013   October 2013   January 2014   March 2014   April 2014   May 2014   July 2014   August 2014   September 2014   October 2014   November 2014   December 2014  

This page is powered by Blogger. Isn't yours?