gpt4 book ai didi

android - 试图重新打开一个已经关闭的对象 : SQLiteDatabase 1212

转载 作者:行者123 更新时间:2023-11-30 02:03:43 25 4
gpt4 key购买 nike

在我的 android 应用程序中,我使用的是 sqlite 数据库,但是当使用查询从 SQLite 读取数据时,它给我错误。我想将结果放在 ListView 中。

这是我的 Activity :

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

我已经卡了一整天了,欢迎任何帮助,谢谢你``

最佳答案

getAllEtudiant() 中,您正在调用 cursorToEtudiant(cursor);,在此方法中,您正在关闭光标 c.close();,因此下一次在循环中关闭游标,只需删除 c.close(),因为您在 getAllEtudiant() 中的 while 循环之后关闭了游标

关于android - 试图重新打开一个已经关闭的对象 : SQLiteDatabase 1212,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31095403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com