android - Sqlite 报错

标签 android sqlite

我正在尝试从数据库中加载数据并将其放入不同的 View 中。

log cat 返回错误,它找不到“_id”列

有人可以帮我解决这个问题吗?

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);
    }
}

SQLAdapter代码:

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;

    }
}

数据实体:

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;
    }
}

来自主要 Activity 的部分:

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

最佳答案

您的适配器正在查找由您的数据库查询返回的名为 _id 的列。您可以将主键更改为 _id,或者当您在数据库上运行选择查询时执行

“SELECT 'selectColumns' 'yourPrimaryKey' as _id from 'yourTableName'”

关于android - Sqlite 报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24110783/

相关文章:

android - 我的 sqlite 数据库存储在 android 中的什么位置?

sql-server - SQL Server 中的 EXPLAIN 形式 SQLite 的等价物是什么?

Android SMSManager sendTextMessage - 我收到 sentIntent 但从未收到 deliveryIntent

android - PowerVR Android 示例 : makefile not working

Android - 我如何*在*进程加载后执行代码

swift - 如何快速将 SQLite 数据库转换为 Realm 数据库

java - "preview unavailabe until after a successful project sync"

android - Android 中的全局域实例

sqlite - 服务帐户在NServiceBus启动时抛出SQLiteException

android - 如何在 Android 中从周日开始从 Sq-lite 检索仅 1 周的数据?