mercredi 23 décembre 2015

insert arabic into SQLite

I've build a database for my android app. It works perfectly with English inputs but when i try to insert Arabic I get this output.

enter image description here

Arabic is the one on top , not sure if the inserting is the problem or the retrieving.

My database class :

public class NewsDataBase {
    private NewsHelper mHelper;
    private static SQLiteDatabase mDatabase;
    private static final String ENCODING_SETTING = "PRAGMA encoding ='windows-1256'";



    public NewsDataBase(Context context) {
        mHelper = new NewsHelper(context);
        mDatabase = mHelper.getWritableDatabase();
    }

    public void insertNews(ArrayList<News> listNews) {

        Log.v("DATABASEFUNFINDER", "insert line befor");

        String sql = "INSERT INTO " + NewsHelper.TABLE_NEWS + " VALUES (?,?,?,?,?,?,?,?);";
        Log.v("DATABASEFUNFINDER", "insert line after");
        SQLiteStatement statement = mDatabase.compileStatement(sql);
        Log.v("DATABASEFUNFINDER", "passing the sql");

        mDatabase.beginTransaction();
        Log.v("DATABASEFUNFINDER", "transaction");
        Log.v("DATABASEFUNFINDER", "" + listNews.size());
        for (int i = 0; i < listNews.size(); i++) {
            Log.v("DATABASEFUNFINDER", "before");

            News currentNews = listNews.get(i);
            statement.clearBindings();

            statement.bindLong(1, currentNews.getId());
            statement.bindString(2, currentNews.getText());
            statement.bindString(3, currentNews.getPlaceId());
            statement.bindString(4, currentNews.getCategory());
            statement.bindString(5, currentNews.getUrlThumbnail());
            statement.bindString(6, currentNews.getPriority());
            statement.bindString(7, currentNews.getSeen());
            statement.bindString(8,currentNews.getExpDate());

            Log.v("DATABASEFUNFINDER", "iteminserted" + i);
            statement.execute();

        }

        mDatabase.setTransactionSuccessful();
        mDatabase.endTransaction();
    }

    public void updateSeen(int id) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(NewsHelper.NEWS_SEEN, "seen");
        mDatabase.update(NewsHelper.TABLE_NEWS, contentValues, NewsHelper.UID + " = " + id, null);

    }


    public ArrayList<News> getAllNews() {
        ArrayList<News> listNews = new ArrayList<>();
        String[] columns = {NewsHelper.UID,
                NewsHelper.NEWS_TEXT,
                NewsHelper.NEWS_PLACEID,
                NewsHelper.NEWS_CATEGORY,
                NewsHelper.NEWS_THUMBNAIL,
                NewsHelper.NEWS_PRIORITY,
                NewsHelper.NEWS_SEEN,
                NewsHelper.NEWS_EXPDATE };


        Cursor cursor = mDatabase.query(NewsHelper.TABLE_NEWS, columns, null, null, null, null, NewsHelper.NEWS_PRIORITY);

        if (cursor != null && cursor.moveToFirst()) {
            do {


                News news = new News();
                news.setId(cursor.getInt(cursor.getColumnIndex(NewsHelper.UID)));
                news.setText(cursor.getString(cursor.getColumnIndex(NewsHelper.NEWS_TEXT)));
                news.setPlaceId(cursor.getString(cursor.getColumnIndex(NewsHelper.NEWS_PLACEID)));
                news.setCategory(cursor.getString(cursor.getColumnIndex(NewsHelper.NEWS_CATEGORY)));
                news.setUrlThumbnail(cursor.getString(cursor.getColumnIndex(NewsHelper.NEWS_THUMBNAIL)));
                news.setSeen(cursor.getString(cursor.getColumnIndex(NewsHelper.NEWS_SEEN)));
                news.setExpDate(cursor.getString(cursor.getColumnIndex(NewsHelper.NEWS_EXPDATE)));
                listNews.add(news);
            }
            while (cursor.moveToNext());

            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }

        }


        Log.v("DATABASEFUNFINDER", "return list news");

        return listNews;


    }

    public News getLastNews() {
        News news = new News();
        String[] columns = {NewsHelper.UID,
                NewsHelper.NEWS_TEXT,
                NewsHelper.NEWS_CATEGORY,
                NewsHelper.NEWS_PRIORITY};

        Cursor cursor = mDatabase.query(NewsHelper.TABLE_NEWS, columns, null, null, null, null, NewsHelper.UID + " DESC");
        if (cursor != null && cursor.moveToFirst()) {

            news.setId(cursor.getInt(cursor.getColumnIndex(NewsHelper.UID)));
            news.setText(cursor.getString(cursor.getColumnIndex(NewsHelper.NEWS_TEXT)));
            news.setCategory(cursor.getString(cursor.getColumnIndex(NewsHelper.NEWS_CATEGORY)));


            Log.v("DATABASEFUNFINDER", "text " + news.getText());

        }
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }

        return news;
    }


    public void deleteAll() {
        mDatabase.delete(NewsHelper.TABLE_NEWS, null, null);
    }
    public void deleteNews(String exp_date){
        String[] whereArg={exp_date};
        mDatabase.delete(NewsHelper.TABLE_NEWS,NewsHelper.NEWS_EXPDATE+" =?",whereArg);}

    public void insertFriendsNews(ArrayList<FriendsNews> listFriendNews,boolean pref) {
        Log.v("databaseface","insert calld");
        if (pref){
            deleteAllFriendsNews();
        }
        String sql = "INSERT INTO " + NewsHelper.TABLE_FRIEND + " VALUES (?,?,?,?);";
        SQLiteStatement statement = mDatabase.compileStatement(sql);

        mDatabase.beginTransaction();
        for (int i = 0; i < listFriendNews.size(); i++) {

            FriendsNews currentNews = listFriendNews.get(i);
            statement.clearBindings();
            Log.v("databasefacebook"," "+currentNews.getFriendName()+" "+currentNews.getPicUrl()+" "+currentNews.getPlaceName()+" "+currentNews.getPlaceUrl());
            statement.bindString(1, currentNews.getFriendName());
            statement.bindString(2, currentNews.getPicUrl());
            statement.bindString(3, currentNews.getPlaceName());
            statement.bindString(4, currentNews.getPlaceUrl());

            statement.execute();

        }

        mDatabase.setTransactionSuccessful();
        mDatabase.endTransaction();
    }

    public ArrayList<FriendsNews> getAllFriendsNews() {
        ArrayList<FriendsNews> listFriendsNews = new ArrayList<>();
        String[] columns = {NewsHelper.FRIEND_NAME,
                NewsHelper.FRIEND_PIC,
                NewsHelper.FRIEND_PLACENAME,
                NewsHelper.FRIEND_PLACELOC,
        };


        Cursor cursor = mDatabase.query(NewsHelper.TABLE_FRIEND, columns, null, null, null, null, null);

        if (cursor != null && cursor.moveToFirst()) {
            do {


                FriendsNews friendsNews = new FriendsNews();
                friendsNews.setFriendName(cursor.getString(cursor.getColumnIndex(NewsHelper.FRIEND_NAME)));
                friendsNews.setPicUrl(cursor.getString(cursor.getColumnIndex(NewsHelper.FRIEND_PIC)));
                friendsNews.setPlaceName(cursor.getString(cursor.getColumnIndex(NewsHelper.FRIEND_PLACENAME)));
                friendsNews.setPlaceUrl(cursor.getString(cursor.getColumnIndex(NewsHelper.FRIEND_PLACELOC)));

                listFriendsNews.add(friendsNews);
            }
            while (cursor.moveToNext());

            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }



        return listFriendsNews;


    }

    public void deleteAllFriendsNews() {
        mDatabase.delete(NewsHelper.TABLE_FRIEND, null, null);
    }


    private static class NewsHelper extends SQLiteOpenHelper {
        private static final String DATABASE_NAME = "funfinderdatabase";
        private static final String TABLE_NEWS = "NEWSTABLE";
        private static final int DATABASE_VERSION = 1;
        private static final String UID = "_id";
        private static final String NEWS_TEXT = "news_text";
        private static final String NEWS_PLACEID = "news_placeId";
        private static final String NEWS_CATEGORY = "news_category";
        private static final String NEWS_THUMBNAIL = "news_thumbnail";
        private static final String NEWS_PRIORITY = "news_priority";
        private static final String NEWS_SEEN = "news_seen";
        private static final String NEWS_EXPDATE = "news_expDate";

        private static final String TABLE_FRIEND = "FRIENDTABLE";
        private static final String FRIEND_NAME = "friend_name";
        private static final String FRIEND_PIC = "fiend_pic";
        private static final String FRIEND_PLACENAME = "friend_placeName";
        private static final String FRIEND_PLACELOC = "friend_placeLoc";
        private static final String CREATE_TABLEFriend = "CREATE TABLE " + TABLE_FRIEND + "("
                + FRIEND_NAME + " Text , "
                + FRIEND_PIC + " Text , "
                + FRIEND_PLACENAME + " Text ,"
                + FRIEND_PLACELOC+" Text );";
        private static final String DROP_TABLEFriend = "DROP TABLE IF EXISTS" + TABLE_FRIEND;

        private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NEWS + "(" + UID + " INTEGER ,"
                + NEWS_TEXT + " Text , "
                + NEWS_PLACEID + " Text , "
                + NEWS_CATEGORY + " Text ,"
                + NEWS_THUMBNAIL + " Text ,"
                + NEWS_PRIORITY + " Text ,"
                + NEWS_SEEN + " Text ,"
                +NEWS_EXPDATE+" Text );";
        private static final String DROP_TABLE = "DROP TABLE IF EXISTS" + TABLE_NEWS;

        public NewsHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            Log.v("DATABASEFUNFINDER", "constructor called");
        }

        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
            try {
                sqLiteDatabase.execSQL(CREATE_TABLE);
                sqLiteDatabase.execSQL(CREATE_TABLEFriend);

                Log.v("DATABASEFUNFINDER", "data base created" + CREATE_TABLE +" "+CREATE_TABLEFriend);
            } catch (android.database.SQLException e) {

                Log.v("DATABASEFUNFINDER", "" + e);
            }


        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {
            try {
                sqLiteDatabase.execSQL(DROP_TABLE);
                sqLiteDatabase.execSQL(DROP_TABLEFriend);

                onCreate(sqLiteDatabase);
                Log.v("DATABASEFUNFINDER", "data base updated");

            } catch (android.database.SQLException e) {
                Log.v("DATABASEFUNFINDER", "" + e);
            }
        }

        @Override
        public void onOpen(SQLiteDatabase db) {
            super.onOpen(db);
            if (!db.isReadOnly()) {
                db.execSQL(ENCODING_SETTING);
            }
        }

        @Override
        public synchronized void close() {
            if(mDatabase != null){
                mDatabase.close();
                super.close();
            }
        }
    }

}

Aucun commentaire:

Enregistrer un commentaire