在我的Android应用程序中,我使用的是sqlite数据库但是当使用查询从SQLite读取数据时它给了我错误。我想把结果放在列表视图中。
这是我的活动:
public class activ5 extends Activity {
ListView etudiants=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activ5);
BDD base2 = new BDD(activ5.this);
base2.open();
ArrayAdapter<Etudiant> etudiantAdapter = new ArrayAdapter<Etudiant>(this, android.R.layout.simple_list_item_activated_1, base2.getAllEtudiant());
etudiants.setAdapter(etudiantAdapter);
base2.close();
这就是我的数据库代码
private static final String CREATE_BDD_ETUDIANT = "CREATE TABLE " + TABLE_ETUDIANT + " ("
+ COL_ID_ETUDIANT + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_NOM + " TEXT NOT NULL, "
+ COL_FILIERE + " TEXT NOT NULL, "+ COL_GROUPE +" TEXT NOT NULL);";
private static final String CREATE_BDD_PROF = "CREATE TABLE " + TABLE_PROF + " ("
+ COL_ID_PROF + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_MODULE + " TEXT NOT NULL, "
+ COL_MATIERE +" TEXT NOT NULL);";
private static final String CREATE_BDD_ABSENCE= "CREATE TABLE " + TABLE_ABSENCE + " ("+ COL_MATIERE_ABSENCE + " TEXT NOT NULL, " + COL_ABSENCE + " INTEGER, "
+ COL_NOM_ABSENCE + " TEXT REFERENCES "+TABLE_ETUDIANT+"("+COL_NOM+")); ";
private static final String CREATE_BDD_COMPTE = "CREATE TABLE " + TABLE_COMPTE + " ("+COL_ID_COMPTE+" INTEGER PRIMARY KEY AUTOINCREMENT, "
+COL_LOGIN+" TEXT NOT NULL,"+COL_PASSWORD+" TEXT NOT NULL,"
+ COL_ID_PROF_COMPTE + " INTEGER REFERENCES "+TABLE_PROF+"("+COL_ID_PROF+"),"
+ COL_ID_ETUDIANT_COMPTE+ " INTEGER REFERENCES "+TABLE_ETUDIANT+"("+COL_ID_ETUDIANT+"));";
private MaBaseDonne(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public static synchronized MaBaseDonne getInstance(Context context) {
if (sInstance == null) {
sInstance = new MaBaseDonne(context.getApplicationContext());
}
return sInstance;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_BDD_ETUDIANT);
db.execSQL(CREATE_BDD_PROF);
db.execSQL(CREATE_BDD_ABSENCE);
db.execSQL(CREATE_BDD_COMPTE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE " + TABLE_PROF + ";");
db.execSQL("DROP TABLE " + TABLE_ETUDIANT + ";");
db.execSQL("DROP TABLE " + TABLE_ABSENCE+ ";");
db.execSQL("DROP TABLE " + TABLE_COMPTE+ ";");
onCreate(db);
}
这是我得到错误的方法:
public List<Etudiant> getAllEtudiant() {
List<Etudiant> etudiants = new ArrayList<Etudiant>();
Cursor cursor = bdd.query(maBaseDonne.TABLE_ETUDIANT,null, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Etudiant etudiant = cursorToEtudiant(cursor);
etudiants.add(etudiant);
cursor.moveToNext();
}
cursor.close();
return etudiants;
}
public void close() {
bdd.close();
}
public Etudiant cursorToEtudiant(Cursor c){
if (c.getCount() == 0)
return null;
c.moveToFirst();
Etudiant etudiant = new Etudiant();
etudiant.setId(c.getInt(NUM_COL_ID_ETUDIANT));
etudiant.setNom(c.getString(NUM_COL_NOM));
etudiant.setFilierere(c.getString(NUM_COL_FILIERE));
etudiant.setGroupe(c.getString(NUM_COL_GROUPE));
c.close();
return etudiant;
这是日志文件:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.radouane.myapplication/com.example.radouane.myapplication.activ5}: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT * FROM table_etudiant
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5349)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT * FROM table_etudiant
at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:58)
at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:152)
at android.database.sqlite.SQLiteCursor.onMove(SQLiteCursor.java:124)
at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:214)
at android.database.AbstractCursor.moveToNext(AbstractCursor.java:245)
at com.example.radouane.myapplication.BDD.getAllEtudiant(BDD.java:244)
at com.example.radouane.myapplication.activ5.onCreate(activ5.java:25)
at android.app.Activity.performCreate(Activity.java:6020)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2284)
最后是所有我的方法的DAO课程
public SQLiteDatabase bdd;
public MaBaseDonne maBaseDonne;
String[] allColumns = {maBaseDonne.COL_NOM,maBaseDonne.COL_GROUPE};
public BDD(Context context) {
maBaseDonne = MaBaseDonne.getInstance(context.getApplicationContext());
}
public void open(){
bdd = maBaseDonne.getWritableDatabase();
}
public void read()
{
bdd=maBaseDonne.getReadableDatabase(); }
public SQLiteDatabase getBDD(){
return bdd;
}
public long insertEtudiant(Etudiant etudiant) {
ContentValues values = new ContentValues();
values.put(COL_NOM, etudiant.getNom());
values.put(COL_FILIERE, etudiant.getFiliere());
values.put(COL_GROUPE, etudiant.getGroupe());
return bdd.insert(TABLE_ETUDIANT, null, values);
}
public long insertProf(Prof prof) {
ContentValues values = new ContentValues();
values.put(COL_MODULE, prof.getModule());
values.put(COL_MATIERE, prof.getMatiere());
return bdd.insert(TABLE_PROF, null, values);
}
public long insertAbsence(Absence absence) {
ContentValues values = new ContentValues();
values.put(COL_NOM_ABSENCE, absence.getNom());
values.put(COL_MATIERE, absence.getMatiere());
values.put(COL_ABSENCE, absence.getAbsence());
return bdd.insert(TABLE_ABSENCE, null, values);
}
public long insertCompte(Compte compte) {
ContentValues values = new ContentValues();
values.put(COL_LOGIN, compte.getLogin());
values.put(COL_PASSWORD, compte.getPassword());
values.put(COL_ID_PROF_COMPTE, compte.getId_prof());
values.put(COL_ID_ETUDIANT_COMPTE, compte.getId_etudiant());
return bdd.insert(TABLE_COMPTE, null, values);
}
public int updateEtudiant(int id, Etudiant etudiant) {
ContentValues values = new ContentValues();
values.put(COL_ID_ETUDIANT, etudiant.getId());
values.put(COL_NOM, etudiant.getNom());
values.put(COL_FILIERE, etudiant.getFiliere());
values.put(COL_GROUPE, etudiant.getGroupe());
return bdd.update(TABLE_ETUDIANT, values, COL_ID_ETUDIANT + " = " + id, null);
}
public int updateProf(int id, Prof prof) {
ContentValues values = new ContentValues();
values.put(COL_ID_PROF, prof.getId());
values.put(COL_MODULE, prof.getModule());
values.put(COL_MATIERE, prof.getMatiere());
return bdd.update(TABLE_ETUDIANT, values, COL_ID_PROF + " = " + id, null);
}
public int updateAbsence(String nom_eleve, Absence absence) {
ContentValues values = new ContentValues();
values.put(COL_NOM_ABSENCE, absence.getNom());
values.put(COL_MATIERE, absence.getMatiere());
values.put(COL_ABSENCE, absence.getAbsence());
return bdd.update(TABLE_ETUDIANT, values, COL_NOM_ABSENCE + " = " + nom_eleve, null);
}
public int updateCompte(String login, Compte compte) {
ContentValues values = new ContentValues();
values.put(COL_LOGIN, compte.getLogin());
values.put(COL_PASSWORD, compte.getPassword());
values.put(COL_ID_PROF_COMPTE, compte.getId_prof());
values.put(COL_ID_ETUDIANT_COMPTE, compte.getId_etudiant());
return bdd.update(TABLE_COMPTE, values, COL_LOGIN + " = " + login, null);
}
public int removeEtudiant(int id){
return bdd.delete(TABLE_ETUDIANT, COL_ID_ETUDIANT + " = " + id, null);
}
public int removeProf(int id){
return bdd.delete(TABLE_PROF, COL_ID_PROF + " = " +id, null);
}
public int removeAbsence(String nom_eleve){
return bdd.delete(TABLE_ABSENCE, COL_NOM_ABSENCE + " = " +nom_eleve, null);
}
public int removeCompte(String login){
return bdd.delete(TABLE_COMPTE, COL_LOGIN + " = " + login, null);
}
public Etudiant getEtudiant(int id){
Cursor c = bdd.query(TABLE_ETUDIANT, new String[] {COL_ID_ETUDIANT, COL_NOM, COL_FILIERE,COL_GROUPE}, COL_ID_ETUDIANT + " LIKE \"" + id +"\"", null, null, null, null);
return cursorToEtudiant(c);
}
public Prof getProf(int id){
Cursor c = bdd.query(TABLE_PROF, new String[] {COL_ID_PROF, COL_MODULE, COL_MATIERE}, COL_ID_PROF + " LIKE \"" + id +"\"", null, null, null, null);
return cursorToProf(c);
}
public Absence getAbsence(String nom_eleve){
Cursor c = bdd.query(TABLE_ABSENCE, new String[] {COL_NOM_ABSENCE, COL_MATIERE_ABSENCE, COL_ABSENCE}, COL_NOM_ABSENCE + " LIKE \"" + nom_eleve +"\"", null, null, null, null);
return cursorToAbsence(c);
}
public Compte getCompte(String login,String password){
Cursor c = bdd.query(TABLE_COMPTE, new String[] {COL_LOGIN, COL_PASSWORD, COL_ID_PROF_COMPTE,COL_ID_ETUDIANT_COMPTE},COL_LOGIN + " LIKE \"" + login +"\""+" and "+COL_LOGIN + " LIKE \"" + login +"\"" , null, null, null, null);
return cursorToCompte(c);
}
public Etudiant cursorToEtudiant(Cursor c){
if (c.getCount() == 0)
return null;
c.moveToFirst();
Etudiant etudiant = new Etudiant();
etudiant.setId(c.getInt(NUM_COL_ID_ETUDIANT));
etudiant.setNom(c.getString(NUM_COL_NOM));
etudiant.setFilierere(c.getString(NUM_COL_FILIERE));
etudiant.setGroupe(c.getString(NUM_COL_GROUPE));
c.close();
return etudiant;
}
private Prof cursorToProf(Cursor c) {
if (c.getCount() == 0)
return null;
c.moveToFirst();
Prof prof = new Prof();
prof.setId(c.getInt(NUM_COL_ID_ETUDIANT));
prof.setModule(c.getString(NUM_COL_MODULE));
prof.setMatiere(c.getString(NUM_COL_MATIERE));
c.close();
return prof;
}
private Absence cursorToAbsence(Cursor c) {
if (c.getCount() == 0)
return null;
c.moveToFirst();
Absence absence = new Absence();
absence.setNom(c.getString(NUM_COL_NOM_ABSENCE));
absence.setMatiere(c.getString(NUM_COL_MATIERE));
absence.setAbsence(c.getInt(NUM_COL_ABSENCE));
c.close();
return absence;
}
private Compte cursorToCompte(Cursor c) {
if (c.getCount() == 0)
return null;
c.moveToFirst();
Compte compte = new Compte();
compte.setLogin(c.getString(NUM_COL_LOGIN));
compte.setPassword(c.getString(NUM_COL_PASSWORD));
compte.setId_prof(c.getInt(NUM_COL_ID_PROF_COMPTE));
compte.setId_etudiant(c.getInt(NUM_COL_ID_ETUDIANT_COMPTE));
return compte;
}
public List<Etudiant> getAllEtudiant() {
List<Etudiant> etudiants = new ArrayList<Etudiant>();
Cursor cursor = bdd.query(maBaseDonne.TABLE_ETUDIANT,null, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Etudiant etudiant = cursorToEtudiant(cursor);
etudiants.add(etudiant);
cursor.moveToNext();
}
cursor.close();
return etudiants;
}
public void close() {
bdd.close();
}
}
我被困了一整天,欢迎任何帮助,谢谢你们。
答案 0 :(得分:1)
在getAllEtudiant()
中,您正在调用cursorToEtudiant(cursor);
,并且在此方法中您正在关闭游标c.close();
,因此下一次在循环中光标关闭时,只需删除{{ 1}}因为你在c.close()
答案 1 :(得分:0)
public List<Etudiant> getAllEtudiant() {
List<Etudiant> etudiants = new ArrayList<Etudiant>();
Cursor cursor = bdd.query(maBaseDonne.TABLE_ETUDIANT,null, null, null, null, null, null);
if(cursor.getCount()!=0 && cursor.moveToFirst()){
do{
Etudiant etudiant = cursorToEtudiant(cursor);
etudiants.add(etudiant);
}while(cursor.moveToNext());
cursor.close();
return etudiants;
}
public void close() {
bdd.close();
}
public Etudiant cursorToEtudiant(Cursor c){
Etudiant etudiant = new Etudiant();
etudiant.setId(c.getInt(NUM_COL_ID_ETUDIANT));
etudiant.setNom(c.getString(NUM_COL_NOM));
etudiant.setFilierere(c.getString(NUM_COL_FILIERE));
etudiant.setGroupe(c.getString(NUM_COL_GROUPE));
c.close();
return etudiant;