mardi 24 février 2015

Error inserting into sqlite databases

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;
}


}


Aucun commentaire:

Enregistrer un commentaire