try and catch while retrieving text form SQLite

半世苍凉 提交于 2020-07-09 12:09:37

问题


I am fetching the SQLite's data in the textView and it is showing following error.

I've also set permission of READ and WRITE_EXTERNAL_STORAGE in mainfest but still I'm getting this error:

this is quotes.java file:

public class quotes extends SQLiteOpenHelper {
    private static final String Database_name = "quotes.db";
    private static final String Database_path = "/data/data/com.birnepal.entrancekoguide/databases/";
    private static final String Table_name = "quotes";
    private static final String quotes = "quotes";
    private static final String uid = "_id";
    private static final int version = 1;
    private Context context;
    public SQLiteDatabase sqlite;

    public void onCreate(SQLiteDatabase sQLiteDatabase) {
    }

    public quotes(Context context2) {
        super(context2, Database_name, null, 1);
        this.context = context2;
        Log.e("Path 1", Database_path);
    }

    public void onUpgrade(SQLiteDatabase sQLiteDatabase, int i, int i2) {
        if (i2 > i) {
            copyDBfromResource();
        }
    }

    public void createDatabase() {
        createDB();
    }

    private void createDB() {
        if (!DBexists()) {
            getReadableDatabase();
            close();
            copyDBfromResource();
        }
    }

    private void copyDBfromResource() {
        String str = "/data/data/com.birnepal.entrancekoguide/databases/quotes.db";
        try {
            InputStream open = this.context.getAssets().open(Database_name);
            FileOutputStream fileOutputStream = new FileOutputStream(str);
            byte[] bArr = new byte[1024];
            while (true) {
                int read = open.read(bArr);
                if (read > 0) {
                    fileOutputStream.write(bArr, 0, read);
                } else {
                    fileOutputStream.flush();
                    open.close();
                    fileOutputStream.close();
                    return;
                }
            }
        } catch (IOException unused) {
            throw new Error("Problem copying database file:");
        }
    }

    public void openDatabase() throws SQLException {
        this.sqlite = SQLiteDatabase.openDatabase("/data/data/com.birnepal.entrancekoguide/databases/quotes.db", null, 0);
        this.sqlite.disableWriteAheadLogging();
    }

    private boolean DBexists() {
        SQLiteDatabase sQLiteDatabase = null;
        try {
            sQLiteDatabase = SQLiteDatabase.openDatabase("/data/data/com.birnepal.entrancekoguide/quotes.db", null, 0);
            sQLiteDatabase.setLocale(Locale.getDefault());
            sQLiteDatabase.setVersion(1);
            sQLiteDatabase.setLockingEnabled(true);
        } catch (SQLException unused) {
            Log.e("Sqlite", "Database not found");
        }
        if (sQLiteDatabase != null) {
            sQLiteDatabase.close();
        }
        if (sQLiteDatabase != null) {
            return true;
        }
        return false;
    }

    public String readQuotes(int i) {
        SQLiteDatabase sQLiteDatabase = this.sqlite;
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT quotes FROM quotes WHERE _id = ");
        sb.append(i);
        sb.append("");
        Cursor rawQuery = sQLiteDatabase.rawQuery(sb.toString(), null);
        return rawQuery.moveToFirst() ? rawQuery.getString(0) : "";
    }

    public boolean insertData(String str) {
        SQLiteDatabase writableDatabase = getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("quotes", str);
        long insert = writableDatabase.insert("quotes", null, contentValues);
        writableDatabase.close();
        return insert != -1;
    }
}

There is quotes in the sqlite and I'm trying to fetch it. It is working perfectly in my phone but in some phone it is not working. I tried try..catch; because if it works it will show the text of SQLite and if it don;t work it will show the text that I write in java. And, try..catch is also not working, same error is shown.

If that SQLite would run in all device it would be great, I want your help.

try {
            this.quotes = new quotes(getContext());
            this.quotes.createDatabase();
            this.quotes.openDatabase();
            this.quotes.getWritableDatabase();
            this.ques = (TextView) view.findViewById(R.id.quotes);
            this.numOfQuotes = 102;
            this.f103i = 1;
            while (true) {
                int i = this.f103i;
                if (i <= this.numOfQuotes) {
                    this.list.add(i);
                    this.f103i++;
                } else {
                    Collections.shuffle(this.list);
                    this.Ques = this.quotes.readQuotes((Integer) this.list.get(this.f104j));
                    this.f104j++;
                    final TextView textView = this.ques;
                    StringBuilder sb = new StringBuilder();
                    sb.append("");
                    sb.append(this.Ques);
                    textView.setText(sb.toString());
                    return;
                }
            }
        }catch (SQLException e)
        {
            ques.setText(R.string.defquote);
        }

来源:https://stackoverflow.com/questions/62531641/try-and-catch-while-retrieving-text-form-sqlite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!