我在DatabaseOpenHelper中创建了一个函数来获取数据库的所有产品列表,但我遇到错误java.lang.IllegalStateException:无效的表
即使我的数据库中有产品表,但仍>
这是我的功能: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; }
这是我尝试运行该应用程序时的错误
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)
这是我的数据库表
我在DatabaseOpenHelper中创建了一个函数来获取数据库的所有产品列表,但是我有错误java.lang.IllegalStateException:即使我的数据库中有产品表,表也无效...
您未在查询构建器上设置表名称。替换
进行这些更改: