Pregunta

I am populating a list view from values retrived from database and I am using cursor adapter to accomplish this task but my issue here is I am getting Illegal argument exception no found column '_ID' but my table has _ID column I am confused on what is the issue as I couldn't understand exactly. below is my code.

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 String column1="Stock_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(){


    }

}

ListView class

public class stockmanager extends Activity{

        String getentry;
    private int storeID=0;
    DatabaseObject d;
    StockTable st;
    private String getstocks;
    private 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);
                poplist populatestocks=new poplist(getApplicationContext(),a1) ;
                popstocks.setAdapter(populatestocks);

            }
        });
                }



    public class poplist extends CursorAdapter{

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


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

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

                }

        }

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

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


    }

    public void Declerations(){


    }


}

Logcat

02-11 16:30:16.903: D/libEGL(4614): loaded /system/lib/egl/libEGL_genymotion.so
02-11 16:30:16.919: D/(4614): HostConnection::get() New Host Connection established 0xb94fd3d0, tid 4614
02-11 16:30:17.011: D/libEGL(4614): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
02-11 16:30:17.015: D/libEGL(4614): loaded /system/lib/egl/libGLESv2_genymotion.so
02-11 16:30:17.395: W/EGL_genymotion(4614): eglSurfaceAttrib not implemented
02-11 16:30:17.411: E/OpenGLRenderer(4614): Getting MAX_TEXTURE_SIZE from GradienCache
02-11 16:30:17.499: E/OpenGLRenderer(4614): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
02-11 16:30:17.503: D/OpenGLRenderer(4614): Enabling debug mode 0
02-11 16:32:10.399: D/libEGL(4800): loaded /system/lib/egl/libEGL_genymotion.so
02-11 16:32:10.495: D/(4800): HostConnection::get() New Host Connection established 0xb941ae00, tid 4800
02-11 16:32:10.543: D/libEGL(4800): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
02-11 16:32:10.571: D/libEGL(4800): loaded /system/lib/egl/libGLESv2_genymotion.so
02-11 16:32:11.039: W/EGL_genymotion(4800): eglSurfaceAttrib not implemented
02-11 16:32:11.071: E/OpenGLRenderer(4800): Getting MAX_TEXTURE_SIZE from GradienCache
02-11 16:32:11.163: E/OpenGLRenderer(4800): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
02-11 16:32:11.179: D/OpenGLRenderer(4800): Enabling debug mode 0
02-11 16:32:13.691: I/Choreographer(4800): Skipped 31 frames!  The application may be doing too much work on its main thread.
02-11 16:32:17.283: E/SQLiteLog(4800): (5) database is locked
02-11 16:32:17.551: E/SQLiteDatabase(4800): Failed to open database '/data/data/com.example.portfoliomanager/databases/PortfolioManager'.
02-11 16:32:17.551: E/SQLiteDatabase(4800): android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:634)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at android.database.sqlite.SQLiteConnection.setJournalMode(SQLiteConnection.java:320)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration(SQLiteConnection.java:294)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:215)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:863)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:235)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at com.example.portfoliomanager.Database.<init>(Database.java:20)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at com.example.portfoliomanager.DatabaseObject.<init>(DatabaseObject.java:12)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at com.example.portfoliomanager.StockTable.<init>(StockTable.java:16)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at com.example.portfoliomanager.Database.onCreate(Database.java:32)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at com.example.portfoliomanager.Database.<init>(Database.java:20)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at com.example.portfoliomanager.DatabaseObject.<init>(DatabaseObject.java:12)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at com.example.portfoliomanager.stockmanager.onCreate(stockmanager.java:33)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at android.app.Activity.performCreate(Activity.java:5133)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at android.os.Looper.loop(Looper.java:137)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at android.app.ActivityThread.main(ActivityThread.java:5103)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at java.lang.reflect.Method.invokeNative(Native Method)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at java.lang.reflect.Method.invoke(Method.java:525)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-11 16:32:17.551: E/SQLiteDatabase(4800):     at dalvik.system.NativeStart.main(Native Method)
02-11 16:32:17.555: I/System.out(4800): database is locked (code 5): , while compiling: PRAGMA journal_mode
02-11 16:32:17.903: D/dalvikvm(4800): GC_FOR_ALLOC freed 125K, 1% free 16925K/17072K, paused 36ms, total 38ms
02-11 16:32:18.215: W/EGL_genymotion(4800): eglSurfaceAttrib not implemented
02-11 16:32:22.199: I/System.out(4800): sdf
02-11 16:32:22.223: I/System.out(4800): Select Max(_ID) from StockTable
02-11 16:32:22.279: I/System.out(4800): 1
02-11 16:32:22.299: I/System.out(4800): Has Values
02-11 16:32:22.383: I/System.out(4800): Select _ID as _ID, StockName From StockTable
02-11 16:32:22.395: D/AndroidRuntime(4800): Shutting down VM
02-11 16:32:22.399: W/dalvikvm(4800): threadid=1: thread exiting with uncaught exception (group=0xa4b99648)
02-11 16:32:22.419: E/AndroidRuntime(4800): FATAL EXCEPTION: main
02-11 16:32:22.419: E/AndroidRuntime(4800): java.lang.IllegalArgumentException: column '_id' does not exist
02-11 16:32:22.419: E/AndroidRuntime(4800):     at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
02-11 16:32:22.419: E/AndroidRuntime(4800):     at android.widget.CursorAdapter.init(CursorAdapter.java:168)
02-11 16:32:22.419: E/AndroidRuntime(4800):     at android.widget.CursorAdapter.<init>(CursorAdapter.java:116)
02-11 16:32:22.419: E/AndroidRuntime(4800):     at com.example.portfoliomanager.stockmanager$poplist.<init>(stockmanager.java:117)
02-11 16:32:22.419: E/AndroidRuntime(4800):     at com.example.portfoliomanager.stockmanager$1.onClick(stockmanager.java:55)
02-11 16:32:22.419: E/AndroidRuntime(4800):     at android.view.View.performClick(View.java:4240)
02-11 16:32:22.419: E/AndroidRuntime(4800):     at android.view.View$PerformClick.run(View.java:17721)
02-11 16:32:22.419: E/AndroidRuntime(4800):     at android.os.Handler.handleCallback(Handler.java:730)
02-11 16:32:22.419: E/AndroidRuntime(4800):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-11 16:32:22.419: E/AndroidRuntime(4800):     at android.os.Looper.loop(Looper.java:137)
02-11 16:32:22.419: E/AndroidRuntime(4800):     at android.app.ActivityThread.main(ActivityThread.java:5103)
02-11 16:32:22.419: E/AndroidRuntime(4800):     at java.lang.reflect.Method.invokeNative(Native Method)
02-11 16:32:22.419: E/AndroidRuntime(4800):     at java.lang.reflect.Method.invoke(Method.java:525)
02-11 16:32:22.419: E/AndroidRuntime(4800):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-11 16:32:22.419: E/AndroidRuntime(4800):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-11 16:32:22.419: E/AndroidRuntime(4800):     at dalvik.system.NativeStart.main(Native Method)
02-11 16:32:25.587: I/Process(4800): Sending signal. PID: 4800 SIG: 9
02-11 16:32:27.335: D/libEGL(4836): loaded /system/lib/egl/libEGL_genymotion.so
02-11 16:32:27.355: D/(4836): HostConnection::get() New Host Connection established 0xb937bb00, tid 4836
02-11 16:32:27.387: D/libEGL(4836): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
02-11 16:32:27.387: D/libEGL(4836): loaded /system/lib/egl/libGLESv2_genymotion.so
02-11 16:32:27.575: W/EGL_genymotion(4836): eglSurfaceAttrib not implemented
02-11 16:32:27.587: E/OpenGLRenderer(4836): Getting MAX_TEXTURE_SIZE from GradienCache
02-11 16:32:27.635: E/OpenGLRenderer(4836): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
02-11 16:32:27.643: D/OpenGLRenderer(4836): Enabling debug mode 0

please help me to solve this.

¿Fue útil?

Solución 3

Is the problem that the _ID is case sensitive? The constant in Android is _ID, but the actual value is "_id". Try utilizing the constant BaseColumns._ID instead of "_ID"

http://developer.android.com/reference/android/provider/BaseColumns.html#_ID

Otros consejos

From your log file i can see that your database is locked and cannot be opened.

Maybe try to fix this before querying it!

If you are on emulator maybe the file is not accessible, you don't have right to open or it's just corrupted ?

In a first attempt, did you make the job with a SimpleCursorAdapter instead of subclassing CursorAdapter ? Maybe it can highlight some obscure parts :)

In a second time, you could enhance your test before iterating over cursor

if(cursor.getCount()!=0){      
        if(cursor.moveToFirst()){
            do{
                //your code

             }while(cursor.moveToNext());
        }                   
}

If you're using a pre-made database (i.e. manually filled up via terminal) then follow this tutorial:

http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

It basically tells you how to get your db linked with Android. Then once this is done you can start making queries i.e.

Cursor cursor = database.query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top