我是android的新手。我从昨天开始就收到了这个错误,令人沮丧,抱歉这个烂摊子。 当我运行这个时,我得到了错误
//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 (?,?)
这是源代码。 公共类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;
}
}
答案 0 :(得分:1)
您的Create SQL Command错误。
+ 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+ "));";
答案 1 :(得分: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 (?,?)
您在CLASES_ASISTENCIA
列中的INTEGER之前缺少“”(空格)。
更改
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 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+ "));";
}
希望这有帮助。
答案 2 :(得分: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