插入sqlite数据库时出错

插入sqlite数据库时出错

我是安卓的新手。从昨天开始我就犯了这个错误,很沮丧,很抱歉弄糟了。
当我运行这个时,我得到了错误

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

错误:
.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 (?,?)

这是源代码。
公共类分支资源{
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;
}

}

最佳答案

Create SQL命令错误。

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

更正:
"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+ "));";

关于android - 插入sqlite数据库时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28711902/

10-12 04:23