0

i have a class with the name of DB Helper .in this class i pass 2 string with the name of current date and offset but i do not know why it cannot open the database and read from it.i think this is because of two query that had limit and current date in them. can any one guide me to fix it? thank

here is my code:

package com.hasti.kamali.amoozeshzaban;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;
import java.util.ArrayList;

public class DataBaseHelper extends SQLiteOpenHelper {

private static String DB_PATH = "/data/data/com.hasti.kamali.amoozeshzaban/databases/";

private String TABLE_NAME = "Words";
private String TABLE_TRANSLATE = "meaningp";
private String TABLE_LESSION = "lesson";
private String KEY_NAME = "word";
private String TABLE_DAY = "day";
private String KEY_ID = "id";
private String TABLE_PREADD = "pron";
ArrayList<WordEntity> list = new ArrayList<>();

String[] allCulomns = {KEY_ID, KEY_NAME, TABLE_TRANSLATE, TABLE_LESSION, TABLE_DAY, TABLE_PREADD};
private static String DB_NAME = "words.sqlite";
public static SQLiteDatabase myDataBase;
private final Context myContext;

public DataBaseHelper(Context context) {
    super(context, DB_NAME, null, 5);
    this.myContext = context;
}

public void createDataBase() throws IOException {

    boolean dbExist = checkDataBase();

    if (dbExist) {            

    } else {

        this.getReadableDatabase();

        try {
            copyDataBase();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

private boolean checkDataBase() {

    SQLiteDatabase checkDB = null;

    try {
        String myPath = DB_PATH + DB_NAME;

        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);

    } catch (SQLiteException e) {
    }

    if (checkDB != null) {
        checkDB.close();
    }

    return checkDB != null ? true : false;
}

private void copyDataBase() throws IOException {

    InputStream myInput = myContext.getAssets().open(DB_NAME);
    String outFileName = DB_PATH + DB_NAME;

    OutputStream myOutput = new FileOutputStream(outFileName);
    byte[] buffer = new byte[1024];

    int length;

    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }

    myOutput.flush();
    myOutput.close();
    myInput.close();
}

public void openDataBase() throws SQLException, IOException {

    try {

    } catch (SQLiteException e) {
        e.printStackTrace();
    }

    String myPath = DB_PATH + DB_NAME;

    File file = new File(myPath);
    if (file.exists() && !file.isDirectory())
        this.myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);

    else
        createDataBase();
}

public void update(WordEntity word) {

    ContentValues values = new ContentValues();
    values.put(TABLE_PREADD, word.getPreadd());
    values.put(TABLE_DAY, word.getDate());
    this.myDataBase.update(TABLE_NAME, values, KEY_ID + " =" + word.getId(), null);
}

@Override

public synchronized void close() {

    if (myDataBase != null)
        myDataBase.close();
        super.close();
}

public Cursor query(String table, String[] columns, String selection,
                    String[] selectionArgs, String groupBy, String having,
                    String orderBy) {
    return myDataBase.query("database", null, null, null, null, null, null);
}

@Override
public void onCreate(SQLiteDatabase db) {
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (newVersion > oldVersion)
        try {
            Log.e("db", "upgrade");
            copyDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }
}

public ArrayList<WordEntity> getAllTodaysWords(String currentdate,String offset) {
    try {
        Cursor cursor = myDataBase.query(TABLE_NAME, allCulomns, TABLE_LESSION+"=1", null, null, null, null, "2,"+offset+"");

        while (cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndex(KEY_ID));
            String title = cursor.getString(cursor.getColumnIndex(KEY_NAME));
            int session = cursor.getInt(cursor.getColumnIndex(TABLE_LESSION));
            String date = cursor.getColumnName(cursor.getColumnIndex(TABLE_DAY));
            String meaning = cursor.getString(cursor.getColumnIndex(TABLE_TRANSLATE));
            String preadd = cursor.getString(cursor.getColumnIndex(TABLE_PREADD));
            Log.e("sami", title);

            list.add(new WordEntity(id, title, meaning, date, session, preadd));
        }

        try {

            Cursor cursor1 = myDataBase.query(TABLE_NAME, allCulomns, TABLE_DAY + "=" + currentdate, null, null, null,null);

            while (cursor1.moveToNext()) {
                int id1 = cursor1.getInt(cursor1.getColumnIndex(KEY_ID));
                String title1 = cursor1.getString(cursor1.getColumnIndex(KEY_NAME));
                int session1 = cursor1.getInt(cursor1.getColumnIndex(TABLE_LESSION));
                String date1 = cursor1.getColumnName(cursor1.getColumnIndex(TABLE_DAY));
                String meaning1 = cursor1.getString(cursor1.getColumnIndex(TABLE_TRANSLATE));
                String preadd1 = cursor1.getString(cursor1.getColumnIndex(TABLE_PREADD));

                list.add(new WordEntity(id1, title1, meaning1, date1, session1, preadd1));
            }
        } catch (Exception e) {
        }
    } catch (Exception e) {
    }
    return list;
}
}
ArK
  • 20,698
  • 67
  • 109
  • 136
hsi
  • 97
  • 1
  • 11

0 Answers0