如何将ID与外键进行比较

时间:2019-01-21 10:58:27

标签: android

我想在android中使用Sqlite进行一对多的关系,例如student_table在Realtion中带有student_informaion表。 Student_table的值填充在Listview中,单击特定的学生姓名后,相应的信息应显示在下一个Listview中。

直到现在我所做的

MainActivity

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Cursor getid=database.getId(); //loading ids to cursor

                ArrayList<Integer> Get_Id = new ArrayList<>();
                Intent content = new Intent(MainActivity.this, Main2Activity.class);
                if (getid!= null && getid.getCount()>0){
                    getid.moveToFirst();
                    do{
                        Get_Id.add(getid.getInt(getid.getColumnIndex("COL1_ID"))); //loading ids to arrays
                    }
                    while(getid.moveToNext());
                }

                    content.putExtra("KEY", Get_Id); //passing ids to next activity
                    startActivityForResult(content,0);  

第二活动

Bundle extras = getIntent().getExtras(); //getting intent
            if(extras!=null) {
                int [] ids = getIntent().getIntArrayExtra("KEY");
                Cursor data =db.rawQuery("select COL3_AYAH  from " + TABLE2_NAME + " where " + COL2_FK+ "=='" + ids + "'" , null);  //selection query
                    ArrayList<String> list = new ArrayList<>();

                    while (data.moveToNext()) {
                        list.add(data.getString(1)); //adding resultant data to listview


                    }
                    ListAdapter listadapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
                    lv.setAdapter(listadapter);

异常详细信息:

android.database.sqlite.SQLiteException: no such column: COL1_ID (Sqlite code 1): , while compiling: select COL1_ID from Surah_Name, (OS error - 2:No such file or directory)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:925)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:536)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:603)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:63)
        at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
        at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1397)
        at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1331)
        at com.hukm_e_azan.al_madrasa.Database.getId(Database.java:82)
        at com.hukm_e_azan.al_madrasa.MainActivity$1.onItemClick(MainActivity.java:53)
        at android.widget.AdapterView.performItemClick(AdapterView.java:321)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1217)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3203)
        at android.widget.AbsListView$3.run(AbsListView.java:4151)
        at android.os.Handler.handleCallback(Handler.java:808)
        at android.os.Handler.dispatchMessage(Handler.java:101)
        at android.os.Looper.loop(Looper.java:166)
        at android.app.ActivityThread.main(ActivityThread.java:7425)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
2019-01-23 15:08:42.577 29192-29192/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.hukm_e_azan.al_madrasa, PID: 29192

1 个答案:

答案 0 :(得分:0)

您似乎正在将整数数组直接传递给查询。尝试遍历数组,以便可以使用实际的索引。 (除非您在查询本身等其他地方出现错误或将Extras设置为null,否则这应该起作用。)

Bundle extras = getIntent().getExtras(); //getting intent
            if(extras!=null) {
                int [] ids = getIntent().getIntArrayExtra("KEY");

                // get each ID from the loop and use it for separate queries
                for (int id : ids) {
                    Cursor data =db.rawQuery("select COL3_AYAH  from " + TABLE2_NAME + " where " + COL2_FK+ "='" + id + "'" , null);  //selection query
                    ArrayList<String> list = new ArrayList<>();

                    while (data.moveToNext()) {
                        list.add(data.getString(1)); //adding resultant data to listview


                    }
                }