Sqlite给出了错误

时间:2014-06-08 21:02:05

标签: android sqlite

我试图从数据库中加载数据并将其放入不同的视图中。

日志cat返回错误,即cannot find "_id" column

有人可以帮我吗?

SqlHelper代码:

public class FiboSqlHelper extends SQLiteOpenHelper {

    public static final String TABLE_FILMDB = "FiboFilmTop250";

    public static final String COLUMN_ID = "_id";

    private static final String DATABASE_NAME = "FiboFilmDb250.sqlite";

    private static final int DATABASE_VERSION = 1;

    public static final String COLUMN_TITLE = "Title";

    public static final String COLUMN_RATING = "Rating";

    public static final String COLUMN_GENRE = "Genre";

    public static final String COLUMN_TIME = "Time";

    public static final String COLUMN_PREMDATE = "PremDate";

    public static final String COLUMN_PLOT = "Plot";

    private static final String DATABASE_CREATE = "create table "
            + TABLE_FILMDB + "(" + COLUMN_ID
            + " integer primary key autoincrement, " + COLUMN_TITLE
            + " text not null " + COLUMN_RATING + " text not null "
            + COLUMN_GENRE + " text not null " + COLUMN_TIME
            + " text not null " + COLUMN_PREMDATE + " text not null "
            + COLUMN_PLOT + " " + "text not null)";

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion,
            int newVersion) {
        // TODO Auto-generated method stub
        Log.w(FiboSqlHelper.class.getName(),
                "Upgrading database from version " + oldVersion
                        + " to " + newVersion
                        + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_FILMDB);
        onCreate(db);
    }
}

SqlAdapterCode:

public class FiboSqlAdapter {

    private SQLiteDatabase database;

    private FiboSqlHelper dbHelper;

    private String[] allColumns = { FiboSqlHelper.COLUMN_ID,
            FiboSqlHelper.COLUMN_TITLE, FiboSqlHelper.COLUMN_GENRE,
            FiboSqlHelper.COLUMN_PREMDATE, FiboSqlHelper.COLUMN_TIME,
            FiboSqlHelper.COLUMN_PLOT };

    public FiboSqlAdapter(Context context) {
        dbHelper = new FiboSqlHelper(context);
    }

    public void open() throws SQLException {
        database = dbHelper.getWritableDatabase();
    }

    public void close() {
        dbHelper.close();
    }

    public List<FilmDataEntity> getAllFilmData() {
        List<FilmDataEntity> fDatas = new ArrayList<FilmDataEntity>();
        Cursor cursor = database.query(FiboSqlHelper.TABLE_FILMDB,
                allColumns, null, null, null, null, null);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            FilmDataEntity fData = cursorToData(cursor);
            fDatas.add(fData);
            cursor.moveToNext();
        }
        cursor.close();
        return fDatas;
    }

    private FilmDataEntity cursorToData(Cursor cursor) {
        FilmDataEntity fData = new FilmDataEntity();
        fData.setId(cursor.getLong(1));
        fData.setTitle(cursor.getString(2));
        fData.setRating(cursor.getString(6));
        fData.setGenre(cursor.getString(4));
        fData.setPremDate(cursor.getString(5));
        fData.setShortcut(cursor.getString(8));

        return fData;

    }
}

DataEntity:

public class FilmDataEntity {

    private long id;

    private String title;

    private String rating;

    private String genre;

    private String premDate;

    private String shortcut;

    public String getShortcut() {
        return shortcut;
    }

    public void setShortcut(String shortcut) {
        this.shortcut = shortcut;
    }

    public String getGenre() {
        return genre;
    }

    public void setGenre(String genre) {
        this.genre = genre;
    }

    public String getPremDate() {
        return premDate;
    }

    public void setPremDate(String premDate) {
        this.premDate = premDate;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getRating() {
        return rating;
    }

    public void setRating(String rating) {
        this.rating = rating;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
}

主要活动的一部分:

List<FilmDataEntity> fE1;
sqA = new FiboSqlAdapter(this);
sqA.open();
fE1 = sqA.getAllFilmData();

1 个答案:

答案 0 :(得分:1)

您的适配器正在查找数据库查询返回的名为_id的列。您可以将主键更改为_id,或者在db do

上运行select查询时

"SELECT 'selectColumns' 'yourPrimaryKey' as _id from 'yourTableName'"