0

Create table statement:

CREATE TABLE product(id integer primary key, name VARCHAR(50), short_desc VARCHAR(100), full_desc VARCHAR(200), internet_url VARCHAR(70), local_url VARCHAR(70), file_name VARCHAR(50), view INT, sum_rating double, image_url VARCHAR(70), number_rating INT )

This is my code to insert product to sqlite:

public void addListProduct(ArrayList<ProductInfo> productList) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    for (ProductInfo product : productList) {
        values.put(KEY_ID, product.getProductId());
        values.put(NAME, product.getName());
        values.put(SHORT_DESC, product.getShortDesc());
        String full = "";
        try {
            full = new String(product.getFullDesc().getBytes(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        values.put(FULL_DESC, full);
        values.put(INTERNET_URL, product.getInternetUrl());
        values.put(FILE_NAME, product.getFileName());
        values.put(VIEW, product.getView());
        values.put(SUM_RATING, product.getRating());
        values.put(IMAGE_URL, product.getImageUrl());
        values.put(NUMBER_RATING, product.getNumberOfRating());

        // insert row
        long indexProduct = db.insert(TABLE_PRODUCT, null, values);
        Log.i("Create product at ", "Create product at " + indexProduct);
    }

}

Before I insert in the table:

Làm sao web tôi được nhiều người biết đến?Làm như thế nào để người khác có thể tìm thấy website của tôi?Tôi tìm những từ khóa trên Google toàn thấy đối thủ cạnh tranh của mình. Làm sao để mình có thể xuất hiện như vậy?Cách làm SEO như thế nào? 

But when get from sqlite: It become:

L?m sao web t?i ???c nhi?u ng??i bi?t ??n?L?m nh? th? n?o ?? ng??i kh?c c? th? t?m th?y website c?a t?i?T?i t?m nh?ng t? kh?a tr?n Google to?n th?y ??i th? c?nh tranh c?a m?nh. L?m sao ?? m?nh c? th? xu?t hi?n nh? v?y?C?ch l?m SEO?nh? th? n?o? 

GetAllProduct:

public List<ProductInfo> getAllProduct() {
    List<ProductInfo> list = new ArrayList<ProductInfo>();
    String selectQuery = "SELECT  * FROM " + TABLE_PRODUCT;

    Log.e("query", selectQuery);

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (c.moveToFirst()) {
        ProductInfo product;
        do {
            product = new ProductInfo();
            product.setProductId(c.getString(c.getColumnIndex(KEY_ID)));
            product.setName(c.getString(c.getColumnIndex(NAME)));
            String shorDesc = "";
            String fullDesc = "";
            try {
                shorDesc = new String(c.getString(
                        c.getColumnIndex(SHORT_DESC))
                        .getBytes("ISO-8859-1"), "UTF-8");

                fullDesc = new String(
                        c.getString(c.getColumnIndex(FULL_DESC)).getBytes(
                                "ISO-8859-1"), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            product.setShortDesc(shorDesc);
            product.setFullDesc(fullDesc);

            product.setLocalUrl(c.getString(c.getColumnIndex(LOCAL_URL)));
            product.setInternetUrl(c.getString(c
                    .getColumnIndex(INTERNET_URL)));
            product.setFileName(c.getString(c.getColumnIndex(FILE_NAME)));
            product.setImageUrl(c.getString(c.getColumnIndex(IMAGE_URL)));
            // product.setCategory(c.getString(c.getColumnIndex(NAME)));
            product.setView(c.getInt(c.getColumnIndex(VIEW)));
            product.setRating(c.getInt(c.getColumnIndex(NUMBER_RATING)));
            product.setNumberOfRating(c.getInt(c
                    .getColumnIndex(NUMBER_RATING)));
            list.add(product);
        } while (c.moveToNext());

    }
    Log.i("Number of record ", list.size() + "");
    return list;
}

1 Answer 1

2

Android sqlite supports unicode strings out of the box. You don't need to do anything yourself to encode characters.

In your code you're trying to interpret ISO-8859-1 byte sequences as UTF-8. Not all characters in your data have a represntation in ISO-8859-1 and the reinterpration is incorrect anyway.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.