1

I am trying to use an already created DB from a csv file using DB Browser. I tried uninstalling and re-installing the app. Also upgraded the DB version and still the same error occurs! Checked my DB path and if the file is being created or not.

DatabaseHelper.java

package com.timespro.bookaseat.Model;

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

import com.timespro.bookaseat.Students;

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


public class DatabaseHelper extends SQLiteOpenHelper {

    private static final String TABLE_NAME1 = "StudentDetails";


    private static final int DB_VERSION = 5;
    private static String DB_NAME = "data.sqlite";
    private  String DB_PATH;
    private  Context mContext;
    private SQLiteDatabase mDataBase;
    private boolean mNeedUpdate = false;


    public DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        this.mContext = context;
        DB_PATH = context.getDatabasePath(DB_NAME).getAbsolutePath();
        Log.d("DB_Path",DB_PATH);
    }

    public void updateDataBase() throws IOException {
        if (mNeedUpdate) {
            File dbFile = new File(DB_PATH + DB_NAME);
            if (dbFile.exists())
                dbFile.delete();


            copyDataBase();

            mNeedUpdate = false;
        }
    }

    /**
     * Check if the database already exist to avoid re-copying the file each time you open the application.
     * @return true if it exists, false if it doesn't
     */
    private boolean checkDataBase(){

        SQLiteDatabase checkDB = null;

        try{
            String myPath = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

        }catch(SQLiteException e){

            Log.d("DB","DB doesn't exist!");

        }

        if(checkDB != null){

            checkDB.close();

        }

        return checkDB != null;
    }


    /**
     * Copies your database from your local assets-folder to the just created empty database in the
     * system folder, from where it can be accessed and handled.
     * This is done by transferring bytestream.
     * */
    private void copyDataBase() throws IOException{
        Log.d("DB","Database copied!");
        //Open your local db as the input stream
        InputStream myInput = mContext.getAssets().open(DB_NAME);

        // Path to the just created empty db
        String outFileName = DB_PATH + DB_NAME;

        //Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);

        //transfer bytes from the input file to the output file
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer))>0){
            myOutput.write(buffer, 0, length);
        }

        //Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();

    }
    /**
     * Creates a empty database on the system and rewrites it with your own database.
     **/
    public void createDataBase() throws IOException{

        boolean dbExist = checkDataBase();

        if(dbExist){
            Log.d("DatabaseHelper","DB already exists!");
            //do nothing - database already exist
        }else{

            //By calling this method and empty database will be created into the default system path
            //of your application so we are gonna be able to overwrite that database with our database.
            this.getReadableDatabase();

            try {

                copyDataBase();

            } catch (IOException e) {

                throw new Error("Error copying database");

            }
        }

    }

    private void copyDBFile() throws IOException {
        Log.d("DB","DB File created on phone");
        InputStream mInput = mContext.getAssets().open(DB_NAME);
        //InputStream mInput = mContext.getResources().openRawResource(getResources().getIdentifier("data","raw", getPackageName()));
        OutputStream mOutput = new FileOutputStream(DB_PATH + DB_NAME);
        byte[] mBuffer = new byte[1024];
        int mLength;
        while ((mLength = mInput.read(mBuffer)) > 0)
            mOutput.write(mBuffer, 0, mLength);
        mOutput.flush();
        mOutput.close();
        mInput.close();
    }

    public void openDataBase() throws SQLException{

        //Open the database
        String myPath = DB_PATH + DB_NAME;
        mDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
        Log.d("DB","DB opened");
    }

    @Override
    public void onConfigure(SQLiteDatabase db) {
        super.onConfigure(db);
        db.setForeignKeyConstraintsEnabled(true);
    }
    @Override
    public synchronized void close() {

        if(mDataBase != null)
            mDataBase.close();

        super.close();

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d("DB","DB created");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (newVersion > oldVersion)
            mNeedUpdate = true;
    }
    public ArrayList<Students> getAllData(){
        ArrayList<Students> students = new ArrayList<>();
        SQLiteDatabase db = this.getWritableDatabase();
        try {
            Cursor res = db.rawQuery("select * from "+TABLE_NAME1, null);
            res.moveToFirst();
            while(res.moveToNext()) {
                int uid = Integer.parseInt(res.getString(0));   //0 is the number of id column in your database table
                String name = res.getString(1);
                String pass = res.getString(2);
                String city = res.getString(3);
                String centre = res.getString(4);
                String inst = res.getString(5);
                String pro = res.getString(6);
                String batch = res.getString(7);
                String days = res.getString(8);
                String time = res.getString(9);

                Students newStudents = new Students(uid,name,pass,city,centre,inst,pro,batch,days,time);
                students.add(newStudents);
            }
            res.close();
        }
        catch(SQLiteException sql){
            Log.d("DatabaseError!","No table found!");
        }
        finally {

            db.close();
        }

        return students;
    }

}

error message: E/SQLiteLog: (1) no such table: StudentDetails D/DatabaseError!: No table found!

2 Answers2

0

If you have the No such table error you might do these things:

A. Try to increment the Database schema.

B. Uninstall the app from the device and install again.

C. Rename the table, there might have been some issue on the background of the SQLite

coroutineDispatcher
  • 7,718
  • 6
  • 30
  • 58
0

The issue could well be that the device is Android Pie or above in which case the default logging has been changed from journal to WAL.

This in conjunction with using

//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();

results in a database being created (so as to create the database directory), this then includes two files for WAL dbname suffixed with -shm and -wal.

When the database is copied the -wal and -shm files do not match the new database (they belong to the overwritten database).

See this for fixes A pre-populated database does not work at API 28 throws “no such table” exception

MikeT
  • 51,415
  • 16
  • 49
  • 68