Sqlite returns error
- by ruruma
I'm trying to implement loading data from database and put it into different views. But log cat returns error, that it cannot find "_id" column. Can somebody help me with this?
SqlHelper Code
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);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
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;
}
}
Part from main activity:
`List<FilmDataEntity> fE1;
sqA = new FiboSqlAdapter(this);
sqA.open();
fE1 = sqA.getAllFilmData();
`