Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

android - Column _ID doesn't exist error though it exists in table

I am repeatedly getting getting java.lang.IllegalArgumentException: column '_id' does not exist and app is getting crashed though I have included _ID in table columns and query.

Here I am trying to extact the data from database and display the listview using custom cursor adapter.

Table class

public class StockTable {

    Context c1;
    DatabaseObject  d1;
    Cursor c;

    StockTable(Context mcontext){
        c1=mcontext;
        //---------------objects---------------------------//
        d1=new DatabaseObject(c1);
        //---------------objects ends----------------------//
    }
    // d1=new DatabaseObject(c1);
    //-----------General Decelerations-------------------//
    private String selectID;
    private int storeID=0;
    //-----------General Decelerations ends--------------//
    //-----------table name and columns-----------------//
    final String tablename="StockTable";
    //public static final String _ID = "ID";
    public String column1=" _ID";
    public String column2="StockName";

    //-----------table name and columns end-----------------//

    final String stocktable = "CREATE TABLE " + tablename + 
                " (" + column1 + " INTEGER PRIMARY KEY , " + column2 + " TEXT ) ";

    public ContentValues insert(String one){

        try{
        selectID="Select Max("+  column1 + ") from " + tablename;
        System.out.println(selectID);
        c=d1.d.db.rawQuery(selectID,null);
        System.out.println(c.getCount());
        //System.out.println(c.getInt(0));
        if(c.moveToNext())
        {

            System.out.println("Has Values");
        }
        else
        {

            System.out.println("No Values");
        }
        }
        catch(Exception e)
        {

            System.out.println(e.getMessage());
        }

        ContentValues cvi=new ContentValues();
    //  for(int i=0;i<=1;i++)
            cvi.put(column1, c.getInt(0)+1);
            cvi.put(column2,one);


        return cvi;
    }

    public void delete(){


    }

    public void select(){


    }

}

Class where list view is populated.

public class stockmanager extends Activity{

    /*public stockmanager() {
        // TODO Auto-generated constructor stub
        populatelist pl=new populatelist();
        //pl.getView(position, getCurrentFocus(), null);
    }*/
    String getentry;
    private int storeID=0;
    DatabaseObject d;
    StockTable st;
    private String getstocks;
    public Cursor a1;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stockmanager);
        d=new DatabaseObject(getApplicationContext());
        st=new StockTable(getApplicationContext());
    final Button AddStock=(Button) findViewById(R.id.button1);
         final EditText entry=(EditText) findViewById(R.id.editText1);
        final Button BroDetail=(Button) findViewById(R.id.button2);
        final ListView popstocks=(ListView) findViewById(R.id.listView1);

        AddStock.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                getentry=entry.getText().toString();
                //st.insert(getentry);
                System.out.println(getentry);
                //d.db.rawQuery(st.select(), null);
                d.d.db.insert(st.tablename, null,st.insert(getentry));
                //populatelist populatestocks=new populatelist();

                getstocks="Select " + st.column1 + " as _ID, " + st.column2 + " From "+ st.tablename;
                 System.out.println(getstocks);
                a1=d.d.db.rawQuery(getstocks, null);
                if(a1.moveToNext()){

                    System.out.println(a1.getCount());  
                }
                else{
                    System.out.println("can't open database");
                }

                poplist populatestocks=new poplist(getApplicationContext(),a1) ;
                popstocks.setAdapter(populatestocks);

            }
        });
                }


    public class poplist extends CursorAdapter{

        public poplist(Context context, Cursor c) {
            super(context, c);
            // TODO Auto-generated constructor stub
        }
        //StockTable st1=new StockTable(getApplicationContext());
        //Database d1=new Database(getApplicationContext());


        @Override
        public void bindView(View view, Context context, Cursor c) {
            // TODO Auto-generated method stub
            final CheckBox cb=(CheckBox) view.findViewById(R.id.checkBox1);
            final Button view1=(Button) view.findViewById(R.id.button1); 

            if(c.moveToFirst()){

                //cb.setText(a1.getString(a1.getColumnIndex(st1.column2)));
                    //do{
                        //cb.setText(a1.getString(a1.getColumnIndexOrThrow(st.column2)));
                cb.setText(c.getString(1));
                //  }while (a1.moveToNext());

                }

        }

        @Override
        public View newView(Context context, Cursor c, ViewGroup parent) {
            // TODO Auto-generated method stub
            LayoutInflater inflater = LayoutInflater.from(context);

            View v = inflater.inflate(R.layout.stocklist, parent, false);
                    bindView(v, context, c);
                   return v;
//          return null;
        }


    }

    public void Declerations(){


    }


}

Please help me to solve this as I am fighting with this for the last 2 days.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As far as Android SQLite is concerned, column names are case sensitive. You need _id not _ID. (In plain SQL the identifiers are not case sensitive.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...