Search code examples
javaandroidmysqlwampserver

SQL query in android doesn't recognize hebrew letters despite in wamp server it does


When i'm trying to find record by hebrew field it doesn't return any data when executing my query. Despite the fact that when i am pasting the same query in my wampserver the query return the rights answer!

I define the table and the fields as utf8_general_ci and still the problem occurred. please help.

public ArrayList<String> findPartnersFast(String semester , String course , String city) {
        String currentQuery = "SELECT * FROM fast_reg " + "WHERE course = " + "'" + "מבוא למקרוכלכלה"  +  "'" ;
        ArrayList<String> arrayOfResults = new ArrayList<String>();
        try {
            statement = connection.createStatement();
            res = statement.executeQuery(currentQuery);
            Log.d("DBmanipulation" , currentQuery);
            while(res.next()) {
                Log.d("DBmanipulation" , "in while loop!");
                String temp = res.getString("student_name");
                arrayOfResults.add(temp);
                Log.d("DBmanipulation", "name: " + temp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (res != null) {
                    res.close();
                }
            } catch (Exception e) {

            }
        }
        return arrayOfResults;
    }

LOG:

03-20 20:29:09.400 3593-4082/com.example.uvalerx073037.finalproject_correct D/Register: In BackGround 
03-20 20:29:09.400 3593-4082/com.example.uvalerx073037.finalproject_correct I/System.out: conneting to Database... 
03-20 20:29:09.532 3593-4082/com.example.uvalerx073037.finalproject_correct I/System.out: Connection Successful 
03-20 20:29:09.550 3593-4082/com.example.uvalerx073037.finalproject_correct D/DBmanipulation: SELECT * FROM fast_reg WHERE course = 'מבוא למקרוכלכלה' 
03-20 20:29:09.557 3593-4082/com.example.uvalerx073037.finalproject_correct I/System.out: connection close properly

Solution

  • You need to make sure that sql table row and field encoded correctly:

    1. the db collation has to be utf8_general_ci.
    2. the collation of the table with hebrew has to be utf8_general_ci
    3. In your connection to the database you need to use setting that will clear the charset, I know that in php the connection setting needs to be like that: ('Content-Type: text/html; charset=utf-8');
    4. in your xhtml tag, in android, you need to define that this page uses utf8:
    5. after selecting the db in the connection script you need to put mysql_query("SET NAMES 'utf8'");

    take a look at this answer also: MySQL db question marks instead of hebrew characters..?