Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

android - column _id does not exist

the error in my logcat is such

Logcat

08-29 08:20:57.961: ERROR/AndroidRuntime(1766): java.lang.RuntimeException: Unable to start activity ComponentInfo{one.two/one.two.Booking}: java.lang.IllegalArgumentException: column '_id' does not exist
08-29 08:20:57.961: ERROR/AndroidRuntime(1766):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766):     at android.app.ActivityThread.access$1800(ActivityThread.java:112)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766):     at android.os.Looper.loop(Looper.java:123)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766):     at android.app.ActivityThread.main(ActivityThread.java:3948)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766):     at java.lang.reflect.Method.invokeNative(Native Method)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766):     at java.lang.reflect.Method.invoke(Method.java:521)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766):     at dalvik.system.NativeStart.main(Native Method)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
08-29 08:20:57.961: ERROR/AndroidRuntime(1766):     at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766):     at android.widget.CursorAdapter.init(CursorAdapter.java:111)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766):     at android.widget.CursorAdapter.<init>(CursorAdapter.java:90)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766):     at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:47)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766):     at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:85)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766):     at one.two.Booking.onCreate(Booking.java:34)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
08-29 08:20:57.961: ERROR/AndroidRuntime(1766):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)

Part of my DBAdapter.java

public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        db = new DBAdapter(this);
        db.open();
        setContentView(R.layout.booking);
        /*long id; 
        id = db.insertTime(
        new String("08:00")); */
        Cursor spinnerCursor = db.getSpinnerData(); 
        startManagingCursor(spinnerCursor);
        Spinner colourSpinner = (Spinner) findViewById(R.id.spinner);

Creation of DB

package one.two;


import java.io.IOException;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter 
{   
    private static String DB_PATH = "/data/data/one.two/databases/"; 
    private static String DB_NAME = "irsyad";

    //values for the login table
    public static final String KEY_ROWID = "_id";
    public static final String KEY_USERNAME = "Username";
    public static final String KEY_PASSWORD = "Password";
    public static final String KEY_LNAME = "LastName";
    public static final String KEY_FNAME ="FirstName";

    public static final String KEY_ROWID2 = "_id";
    public static final String KEY_STATUS = "status";
    public static final String KEY_DESTINATION = "destination";
    public static final String KEY_ARRIVAL = "arrival";
    public static final String KEY_FERRY ="ferry";

    private static final String TAG = "DBAdapter";

    //declare Database name, tables names
    private static final String DATABASE_NAME = "irsyad";
    private static final String DATABASE_TABLE = "User";
    private static final String DATABASE_TABLE_2 = "port";
    private static final int DATABASE_VERSION = 1;

    //declares the rules for the database tables
    private static final String DATABASE_CREATE =
        "create table user (_id integer primary key autoincrement, "
        + "Username text not null, Password text not null,"
        + "LastName text not null, FirstName text not null);";

    private static final String DATABASE_CREATE_2 =
        "create table port (_id integer primary key autoincrement, "
        + "status text null, destination text null,"
        + "arrival text null, ferry text null);";

    private final Context context; 

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx) 
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        //Create the tables with the rules we set.
        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            db.execSQL(DATABASE_CREATE);
            db.execSQL(DATABASE_CREATE_2);
        }

        //OnUpgrade is only for use when u changed the database's version to 2 etc.
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
        int newVersion) 
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion 
                    + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS port");
            onCreate(db);
        }
    }    

    //---opens the database---
    public DBAdapter open() throws SQLException 
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    //---closes the database---    
    public void close() 
    {
        DBHelper.close();
    }

    //Method for inserting login details, can be used in other java files when DBAdapter is
    //declared in the java file. e.g. DBAdapter db = new DBAdapter(this);
    public long insertUser(String Username, String Password, String LastName, String FirstName) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_USERNAME, Username);
        initialValues.put(KEY_PASSWORD, Password);
        initialValues.put(KEY_LNAME, LastName);
        initialValues.put(KEY_FNAME, FirstName);
        return db.insert(DATABASE_TABLE, null, initialValues);
    }
   /* public long insertTime(String arrival) 
    { 
    ContentValues initialValue = new ContentValues(); 
    initialValue.put(KEY_ARRIVAL, arrival); 
    return db.insert(DATABASE_TABLE_2, KEY_ARRIVAL, initialValue); 
    }*/

    //---deletes a particular title---
    public boolean deleteUser(long rowId) 
    {
        return db.delete(DATABASE_TABLE, KEY_ROWID + 
                "=" + rowId, null) > 0;
    }

    //method for retrieving all the inputs from database
    public Cursor getAllUser() 
    {
        return db.query(DATABASE_TABLE, new String[] {
                KEY_ROWID, 
                KEY_USERNAME,
                KEY_PASSWORD,
                KEY_LNAME,
                KEY_FNAME,},
                null, 
                null, 
                null, 
                null, 
                null);
    }   

    public Cursor getAllData() 
    {
        return db.query(DATABASE_TABLE_2, new String[] {
                KEY_ROWID2, 
                KEY_STATUS,
                KEY_DESTINATION,
                KEY_ARRIVAL,
                KEY_FERRY,},
                null, 
                null, 
                null, 
                null, 
                null);
    }   

    //---retrieves a particular title---
    public Cursor getUser(long rowId) throws SQLException 
    {
        Cursor mCursor =
                db.query(true, DATABASE_TABLE, new String[] {
                        KEY_ROWID,
                        KEY_USERNAME, 
                        KEY_PASSWORD,
                        KEY_LNAME,
                        KEY_FNAME,}, 
                        KEY_ROWID + "=" + rowId, 
                        null,
                        null, 
                        null, 
                        null, 
                        null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    public Cursor getSpinnerData() throws SQLException 
    {
        Cursor mCursor =
                db.query(DATABASE_TABLE_2, new String[] {               
                        KEY_ARRIVAL,}, 
                        null,
                        null, 
                        null, 
                        null, 
                        null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    //---updates a title---
    public boolean updateUser(long rowId, String Username, String Password, String LastName, String FirstName)
    {
        ContentValues args = new ContentValues();
        args.put(KEY_USERNAME, Username);
        args.put(KEY_PASSWORD, Password);
        args.put(KEY_LNAME, LastName);
        args.put(KEY_FNAME, FirstName);
        return db.update(DATABASE_TABLE, args, 
                         KEY_ROWID + "=" + rowId, null) > 0;
    }

    private boolean checkDataBase()
    {

        SQLiteDatabase checkDB = null;

        try
        {
            String myPath = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

        }
        catch(SQLiteException e)
        {
            //database does't exist yet.
        }

        if(checkDB != null)
        {
            checkDB.close();
        }

        return checkDB != null ? true : false;
    }

    public void createDataBase() throws IOException
    {

        boolean dbExist = checkDataBase();

        if(dbExist)
        {
            //do nothing - database already exist
        }
        else
        {
             db.execSQL(DATABASE_CREATE);
             db.execSQL(DATABASE_CREATE_2);

        }
    }
}
See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

You are not including _id in your column list for the query you do in getSpinnerData().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...