1

I am newbie at android. I am getting this error since yesterday and it is frustrating, sorry for the mess. When I run this i get the error

            //String texto_a_añadir= et_asistencia.getText().toString();
            //int _texto_a_añadir = Integer.parseInt(texto_a_añadir);
            classRegister(v,1,materiaId);

ERROR:

.example.sebas.myapplication E/SQLiteLog﹕ (1) table clases has no column named asistencia
02-25 00:26:17.658    2210-2210/com.example.sebas.myapplication E/SQLiteDatabase﹕ Error inserting materia_id=9 asistencia=1
    android.database.sqlite.SQLiteException: table clases has no column named asistencia (code 1): , while compiling: INSERT INTO clases(materia_id,asistencia) VALUES (?,?)

this is the sourcecode. public class ClaseDataSource {

public static final String TABLE_CLASES = "clases";
public static final String CLASES_ID = "_id";
public static final String CLASES_MATERIA_ID = "materia_id";
public static final String CLASES_ASISTENCIA = "asistencia";
private final String[] allColumns = {
        CLASES_ID,CLASES_MATERIA_ID,CLASES_ASISTENCIA
};
private SQLiteDatabase database;

public static String createTableQuery(){
    return "CREATE TABLE " + TABLE_CLASES + " ("
            +CLASES_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "
            + CLASES_MATERIA_ID + " INTEGER NOT NULL,"
            + CLASES_ASISTENCIA + "INTEGER DEFAULT 0,"
            + "FOREIGN KEY ("+CLASES_MATERIA_ID+") REFERENCES "
            + MateriaDataSource.TABLE_SUBJECTS + " ("+MateriaDataSource.SUBJECT_ID+ "));";


}

public ClaseDataSource(SQLiteDatabase database){
    this.database = database;
}

public Clase createClase(int asistencia,long materia_id){
    ContentValues values = new ContentValues();
    values.put(CLASES_MATERIA_ID, materia_id);
    values.put(CLASES_ASISTENCIA, asistencia);
    long insertID = database.insert(TABLE_CLASES,null,values);
    Cursor cursor = database.query(TABLE_CLASES,allColumns,CLASES_ID + " = " + insertID, null,null,null,null);
    cursor.moveToFirst();
    Clase newClase = cursorToClase(cursor);
    cursor.close();
    return newClase;
}

public void deleteClase(Clase clase) {
    long id = clase.getId_clase();
    System.out.println("Class deleted with id: " + id);
    database.delete(TABLE_CLASES, CLASES_ID + " = " + id, null);
}

public Clase getClaseById(long id){
    Cursor cursor = database.query(TABLE_CLASES,allColumns,CLASES_ID + " = " + id, null,null,null,null);
    cursor.moveToFirst();
    Clase newClase = cursorToClase(cursor);
    cursor.close();
    return newClase;
}

public List<Clase> getAllClasesByMateria(long id){
    List<Clase> clases = new ArrayList<>();
    Cursor cursor = database.query(TABLE_CLASES, allColumns, CLASES_MATERIA_ID + " = " + id, null,null,null,null,null);
    cursor.moveToFirst();
    while(!cursor.isAfterLast()){
        Clase clase = cursorToClase(cursor);
        clases.add(clase);
        cursor.moveToNext();
    }
    cursor.close();
    return clases;
}

private Clase cursorToClase(Cursor cursor) {
    Clase clase = new Clase();
    clase.setId_clase(cursor.getLong(0));
    clase.setId_materia(cursor.getLong(1));
    clase.setAsistencia(cursor.getInt(2));
    return clase;
}

}

1
  • Your missing a space at the beginning of "INTEGER DEFAULT 0,". Commented Feb 25, 2015 at 5:36

3 Answers 3

1

your Create SQL Command is wrong.

 + CLASES_ASISTENCIA + "INTEGER DEFAULT 0,"// add space before column type

Corrected:

"CREATE TABLE " + TABLE_CLASES + " ("
        +CLASES_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "
        + CLASES_MATERIA_ID + " INTEGER NOT NULL,"
        + CLASES_ASISTENCIA + " INTEGER DEFAULT 0,"
        + "FOREIGN KEY ("+CLASES_MATERIA_ID+") REFERENCES "
        + MateriaDataSource.TABLE_SUBJECTS + " ("+MateriaDataSource.SUBJECT_ID+ "));";
Sign up to request clarification or add additional context in comments.

3 Comments

I believe the table does get created; it just has a column named asistenciaINTEGER. Otherwise, the error would be about a missing table.
I changed it and i continue getting the same error :(. what else could it be?
@Sebas but after changed it you must uninstall your app and install again a fresh apk built.
0
ERROR: .example.sebas.myapplication E/SQLiteLog﹕ (1) table clases has no column named asistencia 02-25 00:26:17.658 2210-2210/com.example.sebas.myapplication E/SQLiteDatabase﹕ Error inserting materia_id=9 asistencia=1 android.database.sqlite.SQLiteException: table clases has no column named asistencia (code 1): , while compiling: INSERT INTO clases(materia_id,asistencia) VALUES (?,?)

You are missing a " " (space) before INTEGER in column CLASES_ASISTENCIA.

Change

public static String createTableQuery(){
    return "CREATE TABLE " + TABLE_CLASES + " ("
            +CLASES_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "
            + CLASES_MATERIA_ID + " INTEGER NOT NULL,"
            + CLASES_ASISTENCIA + "INTEGER DEFAULT 0,"
            + "FOREIGN KEY ("+CLASES_MATERIA_ID+") REFERENCES "
            + MateriaDataSource.TABLE_SUBJECTS + " ("+MateriaDataSource.SUBJECT_ID+ "));";
}

to

public static String createTableQuery(){
    return "CREATE TABLE " + TABLE_CLASES + " ("
            +CLASES_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "
            + CLASES_MATERIA_ID + " INTEGER NOT NULL,"
            + CLASES_ASISTENCIA + " INTEGER DEFAULT 0,"
            + "FOREIGN KEY ("+CLASES_MATERIA_ID+") REFERENCES "
            + MateriaDataSource.TABLE_SUBJECTS + " ("+MateriaDataSource.SUBJECT_ID+ "));";
}

Hope this helps.

Comments

0
    public static String createTableQuery(){
        return "CREATE TABLE " + TABLE_CLASES + " ("
                +CLASES_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "
                + CLASES_MATERIA_ID + " INTEGER NOT NULL,"
                + CLASES_ASISTENCIA + " INTEGER DEFAULT 0,"
                + "FOREIGN KEY ("+CLASES_MATERIA_ID+") REFERENCES "
                + MateriaDataSource.TABLE_SUBJECTS + " ("+MateriaDataSource.SUBJECT_ID+ "));";
    }

// error at here,  "CLASES_ASISTENCIA + "INTEGER DEFAULT 0," need a space befoe integer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.