2015-06-28 73 views
1

在我的android應用程序中,我使用sqlite數據庫,但是當使用查詢從SQLite讀取數據時,它給了我錯誤。我想把結果放在一個列表視圖中。嘗試重新打開已關閉的對象:SQLiteDatabase 1212

這是我的活動:

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

}

我已經beeen堅持全天任何幫助是值得歡迎的,謝謝U``

+1

while循環後關閉遊標你在你的'cursorToEtudiant'一個'close'電話。這意味着在處理第一個項目之後光標將被關閉。 – tachyonflux

+0

當您的查詢得到多於1個結果時,該方法中的moveToFirst調用也會導致無限循環。 – tachyonflux

+0

感謝您的幫助,它的工作 –

回答

1

getAllEtudiant()中您打電話給cursorToEtudiant(cursor);,在此方法中,您正在關閉光標c.close();,所以在循環關閉遊標接下來的時間,只是刪除c.close()因爲你getAllEtudiant()

+0

@RadouaneSefraoui如果這個答案幫助你,請接受答案,閱讀http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – isma3l

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; 
相關問題