mardi 23 février 2016

android.database.sqlite.SQLiteException: no such column: subjectName

when I run this code i get an error saying "E/SQLiteLog: (1) no such column: subjectName". I've seen other posts about this error but none have helped me so far.

 String[] column = new String[] { KEY_SUBJECT };
    String selection = (KEY_DAY + " = ?" + KEY_WEEK + " = ?" + KEY_LESSON + " = ?");

    String[] selectionArgs = new String[] { day, week, lesson};
    Cursor c = ourDatabase.query(DATABASE_TABLE, column, selection, selectionArgs, null, null, null);

    String result = null;
    if (c.moveToFirst()) {
        int sub = c.getColumnIndex(KEY_SUBJECT);
        result = c.getString(sub);
    }
    c.close();

    if (result == null) {
        return false;
    }else{
        return true;
    }

Full Code:

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class TimingDb {

public static final String KEY_ID = "_id";
public static final String KEY_SUBJECT = "subjectName";
public static final String KEY_WEEK = "week";
public static final String KEY_DAY = "dayName";
public static final String KEY_LESSON = "lesson";
public static final String KEY_ROOM = "room";
public static final String KEY_TEACH = "Teach";

private static final String DATABASE_NAME = "TimingDB";
private static final String DATABASE_TABLE = "TimingTable";
private static final int DATABASE_VERSION = 2;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;


private static class DbHelper extends SQLiteOpenHelper {

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        System.out.println(KEY_TEACH);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + KEY_SUBJECT + " TEXT, " + KEY_WEEK + " INTEGER, " + KEY_DAY + " TEXT, " + KEY_LESSON
                + " INTEGER, " + KEY_ROOM + " TEXT, " + KEY_TEACH + "TEXT);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }
}

public TimingDb(Context c) {

    ourContext = c;
}

public TimingDb open() throws SQLException {
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

public void close(){

    ourHelper.close();
}

public void deleteEntries(){
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    ourDatabase.execSQL("DELETE FROM " + DATABASE_TABLE + ";");
}

public long createEntry(String subjectToAdd, int week, String day, int lesson, String room, String Teach) {
    ContentValues contVal = new ContentValues();
    contVal.put(KEY_SUBJECT, subjectToAdd);
    contVal.put(KEY_WEEK, week);
    contVal.put(KEY_DAY, day);
    contVal.put(KEY_LESSON, lesson);
    contVal.put(KEY_ROOM, room);
    contVal.put(KEY_TEACH, Teach);
    return ourDatabase.insert(DATABASE_TABLE, null, contVal);
}

public String fillTimetable(String day, String week, String lesson) {
    String[] column = new String[] { KEY_SUBJECT };
    String selection = KEY_DAY + " = ? " + KEY_WEEK + " = ?" + KEY_LESSON + " = ?";
    String[] selectionArgs = new String[]{ day, week, lesson };
    Cursor c = ourDatabase.query(DATABASE_TABLE, column, selection, selectionArgs, null, null, null);

    String result = "";
    if (c.moveToFirst()) {
        int sub = c.getColumnIndex(KEY_SUBJECT);
        result = c.getString(sub);
    }
    c.close();
    if (result == null) {
        result = "";
        }
    return result;
    }


public String getCurrent(String day, String week, String lesson) {

    String[] column = new String[] {KEY_SUBJECT};
    String selection = KEY_DAY + " = ?" + KEY_WEEK + " = ?" + KEY_LESSON + " = ?";

    String[] selectionArgs = new String[] { day, week, lesson };
    Cursor c = ourDatabase.query(DATABASE_TABLE, column, selection, selectionArgs, null, null, null);

    String result = "";
    if (c.moveToFirst()) {
        int sub = c.getColumnIndex(KEY_SUBJECT);
        result = c.getString(sub);
    }
    c.close();
    if (result == null) {
        result = "";
    }
    return result;
}

public String getRoom(String day, String week, String lesson) {

    String[] column = new String[] {KEY_ROOM};
    String selection = KEY_DAY + " = ?" + KEY_WEEK + " = ?" + KEY_LESSON + " = ?";

    String[] selectionArgs = new String[] { day, week, lesson };
    Cursor c = ourDatabase.query(DATABASE_TABLE, column, selection, selectionArgs, null, null, null);

    String result = "";
    if (c.moveToFirst()) {
        int room = c.getColumnIndex(KEY_ROOM);
        result = c.getString(room);
    }
    c.close();
    if (result == null) {
        result = "";
    }
    return result;
}

public String getTeacher(String day, String week, String lesson) {

    String[] column = new String[] {KEY_TEACH};
    String selection = KEY_DAY + " = ? " + KEY_WEEK + " = ? " + KEY_LESSON + " = ?";

    String[] selectionArgs = new String[] { day, week, lesson };
    Cursor c = ourDatabase.query(DATABASE_TABLE, column, selection, selectionArgs, null, null, null);

    String result = "";
    if (c.moveToFirst()) {
        int teacher = c.getColumnIndex(KEY_TEACH);
        result = c.getString(teacher);
    }
    c.close();
    if (result == null) {
        result = "";
    }
    return result;
}

public boolean doubleEntry(String day, String week, String lesson) {

    String[] column = new String[] { KEY_SUBJECT };
    String selection = (KEY_DAY + " = ?" + KEY_WEEK + " = ?" + KEY_LESSON + " = ?");

    String[] selectionArgs = new String[] { day, week, lesson};
    Cursor c = ourDatabase.query(DATABASE_TABLE, column, selection, selectionArgs, null, null, null);

    String result = null;
    if (c.moveToFirst()) {
        int sub = c.getColumnIndex(KEY_SUBJECT);
        result = c.getString(sub);
    }
    c.close();

    if (result == null) {
        return false;
    }else{
        return true;
    }

}
}

This is the full code for this .java file if that helps

Aucun commentaire:

Enregistrer un commentaire