- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的 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/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!