0

i created a function in DatabaseOpenHelper to get all product list of my database but i have error java.lang.IllegalStateException: Invalid tables

even though i have products table in my database

This is my function : DatabaseOpenHelper

public class DatabaseOpenHelper extends SQLiteAssetHelper {

    private static final String DATABASE_NAME="SKIP.db";
    private  static final int DATABASE_VERSION=1;



    //Constructor

    public DatabaseOpenHelper(Context context){

        super (context, DATABASE_NAME,null,DATABASE_VERSION);

    }


        //Function get all productlist

    public List<productlist> getProductlist()
    {

        SQLiteDatabase db = getReadableDatabase();
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

        //Make sure the same column name in yor database table
        String[] sqlSelect = {"product_name","product_supp","product_category","product_desc"};

        String tableName = "products"; // make sure this is your table name

        qb.getTables();
        Cursor cursor = qb.query(db,sqlSelect,null, null,null,null,null);

        List<productlist> result = new ArrayList<>();
        if(cursor.moveToNext())
        {
            do{
                productlist productlist = new productlist();

                productlist.setId(cursor.getInt(cursor.getColumnIndex("product_id")));
                productlist.setProduct_name(cursor.getString(cursor.getColumnIndex("product_name")));
                productlist.setProduct_supp(cursor.getString(cursor.getColumnIndex("product_supp")));
                productlist.setProduct_category(cursor.getString(cursor.getColumnIndex("product_category")));
                productlist.setProduct_desc(cursor.getString(cursor.getColumnIndex("product_desc")));

                result.add(productlist);
            } while (cursor.moveToNext());
        }
        return result;
    }

This is the error when i tried to run the application

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.skip, PID: 7443
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.skip/com.example.skip.ProductView}: java.lang.IllegalStateException: Invalid tables
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3260)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3396)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2009)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7319)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)
     Caused by: java.lang.IllegalStateException: Invalid tables
        at android.database.sqlite.SQLiteDatabase.findEditTable(SQLiteDatabase.java:1100)
        at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:517)
        at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:392)
        at com.example.skip.DatabaseOpenHelper.getProductlist(DatabaseOpenHelper.java:45)
        at com.example.skip.ProductView.onCreate(ProductView.java:105)
        at android.app.Activity.performCreate(Activity.java:7783)
        at android.app.Activity.performCreate(Activity.java:7772)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3235)

This is my Database table

DATABASE TABLE

1
  • why dont you use ROOM library, it is a great lib by android. Give you compile-time errors, rather than runtime Commented Mar 23, 2020 at 6:37

2 Answers 2

1

You didn't set the table name on the query builder. Replace

qb.getTables();

with

qb.setTables(tableName);
Sign up to request clarification or add additional context in comments.

Comments

0

Make these changes:

Cursor cursor = qb.query(tableName,sqlSelect,null, null,null,null,null);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.