Vietnameses Unicode text in SQLite

639 views Asked by At

I know that this question was asked in this site, but all of answers didn't solved the problem. So I ask again my problem here.

I created a sqlite db (via Firefox Sqlite manager tool), and the data is stored with Vietnamese.

CREATE TABLE "customer" ("cus_id" TEXT, "cus_name" TEXT, "cus_address" TEXT, "cus_phone" TEXT);

INSERT INTO "customer" VALUES('KH1','Trần Văn A','Hai Bà Trưng, quận 1','0908112233');
INSERT INTO "customer" VALUES('KH2','Lê Hoài B','Hai Bà Trưng, quận 1','0908112233');
INSERT INTO "customer" VALUES('KH3','Nguyễn Thành C','Hai Bà Trưng, quận 1','0908112233');

And my app is iOS app. User search text "Tran" I tried the query:

SELECT * FROM customer WHERE cus_name LIKE '%Tran%';

I tried another solution:

SELECT * FROM customer WHERE cus_name COLLATE UTF8CI LIKE '%Tran%';

But I got 0 record. And then I have tried more solutions which I found on internet but I got 0 record too.

Please help. Thank you.

1

There are 1 answers

0
tuledev On

You should create one more field in your database table for the non-unicode Vietnamese value. And you will search in this field.

For removing signs in the Vietnamese string:

#define specialCharacter @[@"áàạảãâấầậẩẫăắằặẳẵ",@"ÁÀẠẢÃÂẤẦẬẨẪĂẮẰẶẲẴ",@"éèẹẻẽêếềệểễ",@"ÉÈẸẺẼÊẾỀỆỂỄ",@"óòọỏõôốồộổỗơớờợởỡ",@"ÓÒỌỎÕÔỐỒỘỔỖƠỚỜỢỞỠ",@"úùụủũưứừựửữ",@"ÚÙỤỦŨƯỨỪỰỬỮ",@"íìịỉĩ",@"ÍÌỊỈĨ",@"đ",@"Đ",@"ýỳỵỷỹ",@"ÝỲỴỶỸ"];
#define replaceCharacter @"aAeEoOuUiIdDyY";

    +(NSString *)removeSign4VietnameseString : (NSString*)str
    {
        NSString *tempStr = str;
        //
        NSArray * _vietNameCharacter = specialCharacter;
        NSString * _replaceCharacter = replaceCharacter;
        for (int i = 0; i<_vietNameCharacter.count; i++) {
            NSString* cRe = [NSString stringWithFormat:@"%C",[_replaceCharacter characterAtIndex:i]];
            for (int j = 0; j<[[_vietNameCharacter objectAtIndex:i] length]; j++) {
                NSString* cOri = [NSString stringWithFormat:@"%C",[[_vietNameCharacter objectAtIndex:i] characterAtIndex:j]];
                tempStr = [tempStr stringByReplacingOccurrencesOfString:cOri withString:cRe];
            }
        }
        return [tempStr lowercaseString];
    }