简体   繁体   中英

How to check (or even set) collation in mdb (ms access) file?

I succesfully migrated Access DB to MySQL DB. All seems to be fine but i need more specific behaviour of MySQL DB.
I need exactly the same sorting of query result as in Access DB after choose option "sort A to Z" .
I use this result in Java application.

MySQL table is UTF-8 and collation is set utf8_general_ci.
Example query is:

"SELECT encode, language FROM suffixes ORDER BY encode COLLATE utf8_unicode_ci"

It returns similar result but there are unacceptable differences in sorting.

Access sorts result like this:

001_01._02.1_02.2.1_02.4_05.e.3.1_07.2.2_15.5.d_20.3.2.1_31.2.2_33.3.4_001
001_01._02.1_02.4.1_06.4.1_06.4.2_07.2.1.1_07.2.2_10.1_11.1.3_20.3.2.1_20.3.7_20.6.8_001

Java sorts result like this:

001_01._02.1_02.2.1_02.4_05.e.3.1_07.2.2_15.5.d_20.3.2.1_31.2.2_33.3.4_001                                                              
001_01._02.1_02.4_06.1_06.2.4.1.2_06.2.4.1.3_06.3.1_07.2.1.1_07.2.2.1_11.2.2_15.2.1.a.1_15.5.a_20.3.2.2.1.a_20.7.1.5_20.8_33.4.5.3_001 

Additionaly I don't know how to check charset and collation in Access.

Can anybody give me a hint what i should do to obtain proper results?

I figured this out. I used Collator class for proper sorting.

FYI it looks like this (this is sorting of ArrayList):

public class AccessSorter
{ 
final static String letters = "< a, A < b, B < c, C < d, D < e, E < f, F < g, G < h, H < i, I < j, J < k, K < l, L < m, M < n, N < o, O < p, P < q, Q < r, R < s, S < t, T < u, U < v, V < w, W < x, X < y, Y < z, Z";
final static String digits = "< 0 < 1 < 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9";
final static String special = "< '.' < '_'";
private RuleBasedCollator accessCollator;


AccessSorter()
{
    try
    {
        accessCollator = new RuleBasedCollator(special + digits + letters);
    }
    catch (ParseException e)
    {
        e.printStackTrace();
    }
}


SomeComparator getSomeComparator()
{
    return new SomeComparator();
}

class SomeComparator implements Comparator<String[]>
{
    @Override
    public int compare(String[] s1, String[] s2)
    {
       return accessCollator.compare(s1[1], s2[1]);
    }
}

}

example of usage:

Collections.sort(someCollectionOfStringArray, new AccessSorter().getSomeComparator()

And now you don't need to worry about collations of access or mySQL. You set rules by yourselve. Of course it takes some time to sort such array.
Remember, if you need to sort big arrays more than once, consider using of getCollatorKey() to increase efficiency.
Also remember if you need to add to collator semicolons, periods, quotation marks put them into ' '.
I hope it will help you somehow. Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM