0

I created an SQLite database, which has a table in it. The database and table can be viewed in SQlite Browser, but when I fire queries, after adding the database to assets folder, it recognizes the database but fails to find the table and gives 'no such table' error.

I am using constructor and calling onCreate() method in constructor; as the 'database open helper inherited class' which I have named as 'FetchRecords' class is not an activity.

What could be wrong with the code? Sharing the code;

1- FetchRecords class :

      package com.example.piechartgenerationfromdatabase;

       import android.app.Activity;
       import android.content.Context;
       import android.database.Cursor;
       import android.database.sqlite.SQLiteDatabase;
       import android.database.sqlite.SQLiteException;
       import android.database.sqlite.SQLiteOpenHelper;

       import java.io.File;
       import java.io.FileOutputStream;
       import java.io.InputStream;
       import java.io.OutputStream;

       public class FetchRecords extends SQLiteOpenHelper {
          private static final int DATABASE_VERSION = 3;
          private static final String DATABASE_NAME = "AlarmDataSqliteFinal";
          private static final String TABLE_NAME = "AlarmDataTable";

    Cursor cursor = null;
    Cursor cursor1 = null;
    SQLiteDatabase db = null;

    String[] distinctAlarmNames;
    String[] allValuesInColumnAlarmNames;
    int[] occurancesOfDistinctAlarmNames;
    String[] topFiveAlarmNames;
    int[] temp;
    int totalNumberOfValuesInAlarmNamesColumn;

    public FetchRecords(Context context) {
        super(context, "AlarmDataSqliteFinal", null, DATABASE_VERSION);
        onCreate(this.getReadableDatabase());
    }

    public String[] getTopFiveAlarmNames(){
        return this.topFiveAlarmNames;
    }


    public String[] getDistinctAlarmNames(){
        return this.distinctAlarmNames;
    }
    public int[] getOccurancesOfDistinctAlarmNames(){
        return this.occurancesOfDistinctAlarmNames;
    }

    @Override
    public void onCreate(SQLiteDatabase db){
            //creating queries
            String numberOfRowsInDatabaseCalculatingQuery = "SELECT * FROM AlarmDataTable";
            String distinctQuery = "SELECT DISTINCT AlarmName FROM AlarmDataTable";

            //getting permission to read database
            db = this.getReadableDatabase();
            System.out.println(db.toString());

            //Firing queries
            cursor = db.rawQuery(distinctQuery, null); //Here I get the exception
            int numberOfDistinctRecords = cursor.getCount();

            //instantiating arrays
            distinctAlarmNames  = new String[numberOfDistinctRecords];
            allValuesInColumnAlarmNames = new String[totalNumberOfValuesInAlarmNamesColumn];
            occurancesOfDistinctAlarmNames = new int[numberOfDistinctRecords];

            //inserting values in arrays
            int indexOfDistinctNamesArray = 0;
            int  indexOfAllValuesInColumnAlarmNamesArray = 0;
            while(cursor.moveToNext()){
                distinctAlarmNames[indexOfDistinctNamesArray] = cursor.getString(cursor.getColumnIndex("AlarmNames"));
                indexOfDistinctNamesArray++;
            }
            cursor.close();

            cursor1 = db.rawQuery(numberOfRowsInDatabaseCalculatingQuery,null);

            totalNumberOfValuesInAlarmNamesColumn = cursor1.getCount();

            while(cursor1.moveToNext()){
                allValuesInColumnAlarmNames[indexOfAllValuesInColumnAlarmNamesArray] = cursor.getString(cursor.getColumnIndex("AlarmNames"));
                indexOfAllValuesInColumnAlarmNamesArray++;
            }
            cursor1.close();
            //calculating top five alarms and their ocuurences
            int occurenceVariable = 0;
            int indexOfOccurenceArray=0;
            for(int i=0; i<numberOfDistinctRecords;i++) {
                for (int j = 0; j < totalNumberOfValuesInAlarmNamesColumn; j++) {
                    if (distinctAlarmNames[i].equalsIgnoreCase(allValuesInColumnAlarmNames[j])) {
                        occurenceVariable++;
                    }
                }
                occurancesOfDistinctAlarmNames[indexOfOccurenceArray] = occurenceVariable;
                occurenceVariable = 0;
                indexOfOccurenceArray++;
            }

            temp = new int[occurancesOfDistinctAlarmNames.length];
            for(int i=0; i<occurancesOfDistinctAlarmNames.length;i++){
                temp[i] = occurancesOfDistinctAlarmNames[i];
            }

            //calculating top five largest occurences
            int topFiveOccurances[] = new int[5];
            int largest = 0;
            for(int unsortedIndex=1; unsortedIndex<5;unsortedIndex++){
                for(int sortedIndex=0; sortedIndex<unsortedIndex; sortedIndex++){
                    if(occurancesOfDistinctAlarmNames[unsortedIndex] > occurancesOfDistinctAlarmNames[sortedIndex]){
                        int temp = occurancesOfDistinctAlarmNames[sortedIndex];
                        occurancesOfDistinctAlarmNames[sortedIndex] = occurancesOfDistinctAlarmNames[unsortedIndex];
                        occurancesOfDistinctAlarmNames[unsortedIndex] = temp;
                    }
                }
            }

            for(int i=0; i<5;i++){
                topFiveOccurances[i] = occurancesOfDistinctAlarmNames[i];
            }
            int alarmnamestopfiveindex = 0;

            for(int i=0; i<occurancesOfDistinctAlarmNames.length;i++){
                for(int j=0; j<5;j++){
                    if(temp[i] == topFiveOccurances[j]){
                        topFiveAlarmNames[alarmnamestopfiveindex] = distinctAlarmNames[i];
                        alarmnamestopfiveindex++;
                    }
                }
            }
        System.out.println("temp length"+temp.length);
        //System.out.println("");
        }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

2- Activity that calls the FetchRecords class :

  package com.example.piechartgenerationfromdatabase;

     import android.content.Intent;
     import android.os.Bundle;

     import androidx.appcompat.app.AppCompatActivity;

     public class PieChartActivity extends AppCompatActivity {
         PieChartView pieChart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //fetching records
        //Intent fetchRecords = new Intent(PieChartActivity.this,FetchRecords.class);
        //startActivity(fetchRecords);
        FetchRecords fetch = new FetchRecords(this.getApplicationContext());
        pieChart = new PieChartView(this,fetch.getTopFiveAlarmNames(),fetch.temp);
        setContentView(pieChart);
    }
   }
mohammadReza Abiri
  • 1,759
  • 1
  • 9
  • 20

1 Answers1

0

You are accessing database that located in /data/data/com.example.piechartgenerationfromdatabase/databases and not the one that in the assets folder.

This answer might be helpful: how to put database and read database from assets folder android which are created and exported in sqllite

mpx5
  • 51
  • 1
  • 4