Android SQLite首次使用:错误java.lang.IllegalArgumentException:column' _id'不存在

时间:2017-04-03 14:16:55

标签: java android sqlite android-sqlite

我知道,已经有很多答案了。但我还是一个使用SQLite的新手,我尝试了那些答案,但没有任何效果。

它说我的列ID不存在,但确实存在:

public class SQLite extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "OMDBAPI";

// Films table name
static final String TABLE_FILMS = "films";

// Contacts Table Columns names
static final String KEY_TITLE = "Title";
static final String KEY_YEAR = "Year";
static final String KEY_RELEASED = "Released";
static final String KEY_RUNTIME = "Runtime";
static final String KEY_GENRE = "Genre";
static final String KEY_DIRECTOR = "Director";
static final String KEY_WRITER = "Writer";
static final String KEY_ACTORS = "Actors";
static final String KEY_PLOT = "Plot";
static final String ID = "_id";

public SQLite(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_FILMS_TABLE = "CREATE TABLE " + TABLE_FILMS +"(" +
            ID + "integer primary key autoincrement," +
            KEY_TITLE + " TEXT, " +
            KEY_YEAR + " TEXT, " +
            KEY_RELEASED + " TEXT, " +
            KEY_RUNTIME + " TEXT, " +
            KEY_GENRE + " TEXT, " +
            KEY_DIRECTOR + " TEXT, " +
            KEY_WRITER + " TEXT, " +
            KEY_ACTORS + " TEXT, " +
            KEY_PLOT + " TEXT" +
            ");";
    db.execSQL(CREATE_FILMS_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_FILMS);

    // Create tables again
    onCreate(db);

}
}

我尝试在列表视图中填充它:

public class Query extends Activity {

        private ListView list;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.list_view);

            DBController crud = new DBController(getBaseContext());
            Cursor cursor = crud.loadData();

            String[] titles = new String[] {SQLite.ID, SQLite.KEY_TITLE};
            int[] idViews = new int[] {R.id.idnumber, R.id.grid_title};

            SimpleCursorAdapter adapter = new SimpleCursorAdapter(getBaseContext(),
                    R.layout.adapter_layout,cursor,titles,idViews, 0);
            list = (ListView)findViewById(R.id.listView);
            list.setAdapter(adapter);
        }
}

这是我的DB CONTROLLER(新手的错误?我也应该把ID放在这里吗?):

public class DBController {

    private SQLiteDatabase db;
    private SQLite bank;

    String success = "Film saved.";
    String failed = "There was a problem saving the movie.";

    public DBController(Context context){
        bank = new SQLite(context);
    }

    public String insertData(String title, String release, String year, String writers,  String actors, String director, String genre, String plot, String runtime){
        ContentValues values;
        long result;

        db = bank.getWritableDatabase();
        values = new ContentValues();
        //  Insert values
        values.put(SQLite.KEY_TITLE, title);
        values.put(SQLite.KEY_YEAR, release);
        values.put(SQLite.KEY_RELEASED, release);
        values.put(SQLite.KEY_YEAR, year);
        values.put(SQLite.KEY_GENRE, genre);
        values.put(SQLite.KEY_DIRECTOR, director);
        values.put(SQLite.KEY_WRITER, writers);
        values.put(SQLite.KEY_ACTORS, actors);
        values.put(SQLite.KEY_PLOT, plot);
        values.put(SQLite.KEY_RUNTIME, runtime);

        result = db.insert(SQLite.TABLE_FILMS, null, values);
        db.close();

        if (result ==-1)
            return failed;
        else
            return success;

    }

    public Cursor loadData(){
        Cursor cursor;
        String[] fields =  {bank.KEY_TITLE,bank.KEY_RELEASED};
        db = bank.getReadableDatabase();
        cursor = db.query(bank.TABLE_FILMS, fields, null, null, null, null, null, null);

        if(cursor!=null){
            cursor.moveToFirst();
        }
        db.close();
        return cursor;
    }
}

有人可以帮我找出为什么说我没有ID栏吗?

(为什么ppl downvote post?我认为Stack Overflow是为了帮助像我这样的白痴= P)

2 个答案:

答案 0 :(得分:4)

您没有_id列,您有_idinteger,因为您忘记在CREATE查询中添加空格。改变行

ID + "integer primary key autoincrement," +

ID + " integer primary key autoincrement," +

答案 1 :(得分:-2)

您可以使用baseColums界面提供主键字段(称为_ID
example from google