The full error I'm getting is this:
java.lang.RuntimeException: Unable to start activity ComponentInfo{}: android.database.sqlite.SQLiteException: no such column: Cowboy Bebop(code 1): , while compiling: SELECT * FROM anime WHERE title = Cowboy Bebop
Also I get this:
Caused by: android.database.sqlite.SQLiteException: no such column: Cowboy Bebop(code 1): , while compiling: SELECT * FROM anime WHERE title = Cowboy Bebop
Here is my DatabaseHelper Class:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "trackedAnime";
//Create Tables
private static final String TABLE_ANIME = "anime";
private static final String TABLE_SCORES = "scores";
//Common column
private static final String KEY_TITLE = "title";
private static final String KEY_SCORE = "score";
//Anime Table
private static final String KEY_SYNOPSIS = "synopsis";
private static final String KEY_SEARCH = "search";
private static final String KEY_IMAGE = "image";
//Score Table
private static final String KEY_DATE = "date";
//Create statements
private static final String CREATE_TABLE_ANIME = "create table "+
TABLE_ANIME + "(" + KEY_TITLE + " TEXT PRIMARY KEY," + KEY_SCORE +
" DOUBLE," + KEY_SYNOPSIS + " TEXT," + KEY_SEARCH + " TEXT," +
KEY_IMAGE + " TEXT)";
private static final String CREATE_TABLE_SCORES = "create table "+
TABLE_SCORES + "(" + KEY_TITLE + " TEXT," +
KEY_SCORE + " DOUBLE," + KEY_DATE +" TEXT, PRIMARY KEY ("+KEY_TITLE+", "+
KEY_SCORE+", "+KEY_DATE+"))";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// db.execSQL("DROP TABLE IF EXISTS " + TABLE_ANIME);
// db.execSQL("DROP TABLE IF EXISTS " + TABLE_SCORES);
db.execSQL(CREATE_TABLE_ANIME);
db.execSQL(CREATE_TABLE_SCORES);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ANIME);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_SCORES);
onCreate(db);
}
//Add an Anime to the anime table
public long addAnime(AnimeData data){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TITLE, data.gettitle());
values.put(KEY_SCORE, data.getScore());
values.put(KEY_IMAGE, data.getImage());
values.put(KEY_SEARCH, data.getSearch());
values.put(KEY_SYNOPSIS, data.getSynopsis());
long insert = db.insert(TABLE_ANIME, null, values);
return insert;
}
//Add score to score table
public long addScore(AnimeScores scores){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TITLE, scores.getTitle());
values.put(KEY_SCORE, scores.getScore());
values.put(KEY_DATE, scores.getDate());
long insert = db.insert(TABLE_SCORES, null, values);
return insert;
}
//Retrieve dates and scores for a tracked anime
public List<AnimeScores> getScores(String title){
List<AnimeScores> scores = new ArrayList<AnimeScores>();
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT * FROM " + TABLE_SCORES + " WHERE " + KEY_TITLE + " = " + title;
Log.e(DatabaseHelper.class.getName(), query);
Cursor c = db.rawQuery(query,null); //GETTING ERROR HERE
if(c.moveToFirst()){
do {
AnimeScores score = new AnimeScores();
score.setTitle(c.getString(c.getColumnIndex(KEY_TITLE)));
score.setScore(c.getDouble(c.getColumnIndex(KEY_SCORE)));
score.setDate(c.getString(c.getColumnIndex(KEY_DATE)));
scores.add(score);
} while (c.moveToNext());
}
c.close();
return scores;
}
//Get specific tracked anime
public AnimeData getAnime(String title){
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT * FROM " + TABLE_ANIME + " WHERE " + KEY_TITLE + " = " + title;
Log.e(DatabaseHelper.class.getName(), query);
Cursor c = db.rawQuery(query,null); //GETTING ERROR HERE
if(c!=null){
c.moveToFirst();
}
AnimeData show = new AnimeData();
show.settitle(c.getString(c.getColumnIndex(KEY_TITLE)));
show.setSynopsis(c.getString(c.getColumnIndex(KEY_SYNOPSIS)));
show.setSearch(c.getString(c.getColumnIndex(KEY_SEARCH)));
show.setImage(c.getString(c.getColumnIndex(KEY_IMAGE)));
show.setScore(c.getDouble(c.getColumnIndex(KEY_SCORE)));
c.close();
return show;
}
public void closeDB(){
SQLiteDatabase db = this.getReadableDatabase();
if(db!=null && db.isOpen()){
db.close();
}
}
}
When I call the methods db.getAnime(title) and db.getScores(title) in an activity, they both crash on the cursor lines. I'm not really sure why. I am able to insert correcly but when I run these 2 query's, my app crashes.
Aucun commentaire:
Enregistrer un commentaire