android:无法将db的语言环境更改为“en_US”

时间:2015-10-31 19:54:23

标签: android

嗯,此错误仅出现在某些设备上,而在其他设备上出现的数据库正常。我对这个错误一无所知 我的db文件。我试图做的是删除db文件并重新创建它,但这没有帮助。某些设备上仍有问题。 错误是

10-31 22:33:04.330 9451-9451/com.kr.moi E/SQLiteLog: (11) database corruption at line 52968 of [9491ba7d73]
10-31 22:33:04.330 9451-9451/com.kr.moi E/SQLiteLog: (11) database corruption at line 53007 of [9491ba7d73]
10-31 22:33:04.330 9451-9451/com.kr.moi E/SQLiteLog: (11) statement aborts at 15: [SELECT locale FROM android_metadata UNION SELECT NULL ORDER BY locale DESC LIMIT 1] 
10-31 22:33:04.332 9451-9451/com.kr.moi E/SQLiteDatabase: Failed to open database '/data/data/com.kr.moi/databases/links_db.db'.
10-31 22:33:04.332 9451-9451/com.kr.moi E/SQLiteDatabase: android.database.sqlite.SQLiteException: Failed to change locale for db '/data/data/com.kr.moi/databases/links_db.db' to 'en_US'.

我不知道这个设备上发生了什么(一加2 :: android 5.1.1)。有什么建议吗?

public class SQLiteDB extends SQLiteOpenHelper {
    private SQLiteDatabase myDataBase;
    private final Context context;
    private String DB_PATH;
    private String myPath;
    public static int fav_q = 0, cou_q = 0;
    private boolean isCreateDatabase = false;
    private boolean isUpgradeDatabase = false;
    private static String db_name = "links_db.db", tableName = "links_tb";

    public SQLiteDB(Context context) {
        super(context, db_name, null, 1);
        this.context = context;
        String path = context.getFilesDir().getPath().replace("files", "");
        DB_PATH = path + "/databases/";
        myPath = DB_PATH + db_name;
    }

    public void initializeDataBase() {
        this.getWritableDatabase();
        if (isCreateDatabase) {
            copyDataBase();
        } else if (isUpgradeDatabase) {
            context.deleteDatabase(db_name);
            copyDataBase();
        }
    }

    private void copyDataBase() {
        InputStream myInput = null;
        OutputStream myOutput = null;
        try {
            // close();
            myInput = context.getAssets().open(db_name);
            myOutput = new FileOutputStream(myPath);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0) {
                myOutput.write(buffer, 0, length);
            }
        } catch (Exception e) {
            Log.d(getClass().getName(), "copyDataBase : " + e);
        } finally {
            try {
                myOutput.flush();
                myOutput.close();
                myInput.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onCreate(SQLiteDatabase arg0) {
        isCreateDatabase = true;
    }

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
        isUpgradeDatabase = true;
    }

    public void openDataBase() {
        try {
            if (myDataBase == null) {
                myDataBase = this.getWritableDatabase();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public synchronized void close() {
        if (myDataBase != null&&myDataBase.isOpen())
            myDataBase.close();
        super.close();
    }
    public ArrayList<ContentBean> getLinks(boolean isEnglish) {
        ArrayList<ContentBean> contentList = new ArrayList<ContentBean>();
        Cursor cursor = null;
        try {
            if(isEnglish)
                cursor = this.myDataBase.rawQuery("select name_en,link_en,desc_en from " + tableName    + " order by id",   null);
            else
                cursor = this.myDataBase.rawQuery("select name_ar,link_ar,desc_ar from " + tableName    + " order by id",   null);

            if (cursor.getCount() > 0) {
                int name,desc, link;

                if(isEnglish){
                    name = cursor.getColumnIndex("name_en");
                    desc = cursor.getColumnIndex("desc_en");
                    link = cursor.getColumnIndex("link_en");
                }else{
                    name = cursor.getColumnIndex("name_ar");
                    desc = cursor.getColumnIndex("desc_ar");
                    link = cursor.getColumnIndex("link_ar");
                }
                ContentBean bean = new ContentBean();
                for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
                        .moveToNext()) {
                    if (!cursor.isFirst())
                        bean = bean.cloneMe();
                    bean.setTitle(cursor.getString(name).toString().trim());
                    bean.setDesc(cursor.getString(desc).toString().trim());
                    bean.setLink(cursor.getString(link));
                    contentList.add(bean);
                }
            }
        } catch (Exception e) {
            Log.e(getClass().getName(), "getLinks : " + e.getMessage());
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }
        return contentList;
    }
}

0 个答案:

没有答案