gpt4 book ai didi

android - Sqlite 报错

转载 作者:行者123 更新时间:2023-11-30 02:51:17 27 4
gpt4 key购买 nike

我正在尝试从数据库中加载数据并将其放入不同的 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com