在我的Android应用程序中,我使用的是sqlite数据库但是当使用查询从SQLite读取数据时,它给了我错误。这是我创建表的代码。
package com.example.mydb;
public class Dbadapter {
private static final String DATABASE_NAME = "mydb";
private static final int DATABASE_VERSION = 5;
private static final String DATABASE_TABLE18 = "modalation_medicine_table";
private static final String MOD_ID = "modalation_previousss_id";
private static final String MOD_MED_ID = "modalation_medicine_id";
private static final String MOD_MEDICINE = "modalation_medicine";
private static final String CREATE_DATABASE_TABLE18 = "CREATE TABLE IF NOT EXISTS "
+ DATABASE_TABLE18
+ " ("
+ MOD_MED_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ MOD_MEDICINE
+ " text, "
+ MOD_ID
+ " integer, "
+ " FOREIGN KEY ("
+ MOD_ID
+ ") REFERENCES "
+ DATABASE_TABLE5
+ " ("
+ MODALATION_KEY_ID
+ "));";
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_DATABASE_TABLE18);
ContentValues modalation_medicine = new ContentValues();
modalation_medicine.put(MOD_MEDICINE, "Natrum Muriaticum");
modalation_medicine.put(MOD_ID, 1);
db.insert(DATABASE_TABLE18, null, modalation_medicine);
modalation_medicine.put(MOD_MEDICINE, "Calcarea Carbonica");
modalation_medicine.put(MOD_ID, 1);
db.insert(DATABASE_TABLE18, null, modalation_medicine);
modalation_medicine.put(MOD_MEDICINE, "Phosphorus");
modalation_medicine.put(MOD_ID, 1);
db.insert(DATABASE_TABLE18, null, modalation_medicine);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE18);
}
}
public Dbadapter(Context c) {
ourContext = c;
}
public Dbadapter open() throws SQLException{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close() {
ourHelper.close();
}
现在,当我使用此方法从我的sqLite数据库中读取数据时,它会给出一个错误“无法重新打开已经关闭的对象”。
public boolean mod_app_symps(String l, String modfinalMed) {
String[] modalation = new String[] { MOD_ID, MOD_MEDICINE };
Cursor c24;
boolean iscontain = false;
c24 = ourDatabase.query(DATABASE_TABLE18, modalation, "modalation_previousss_id = ? AND modalation_medicine = ?",new String[] { l , modfinalMed } , null, null, null);
if(c24.getCount() > 0) {
iscontain = true;
}
return iscontain;
}
我试图通过谷歌解决问题,但没有任何帮助! P.S:我有许多其他方法正在做同样的工作,即从SQLite数据库读取数据,它们都工作正常。该错误仅在此方法上出现。
编辑:此方法正常
public String[] getmodMed(long l) {
String[] modalation_med = new String[] { MOD_ID, MOD_MEDICINE};
Cursor c19 = ourDatabase.query(DATABASE_TABLE18, modalation_med, MOD_ID + "='" + l + "'" , null, null, null, null);
String mod_medicine[]= new String[c19.getCount()];
int imodmedicine = c19.getColumnIndex(MOD_MEDICINE);
if(c19 != null){
c19.moveToFirst();
while(!c19.isAfterLast()) {
for(int i =0; i< c19.getCount(); i++) {
mod_medicine[i]= c19.getString(imodmedicine);
c19.moveToNext();
} }
}
return mod_medicine;
}
答案 0 :(得分:0)
这是因为这行代码:
public void close() {
ourHelper.close();
}
您关闭数据库,然后想再次访问它。
答案 1 :(得分:0)
我们需要查看更多代码,特别是&#34; ourDatabase&#34;。听起来它在你运行该方法之前就已经关闭了。尝试在查询之前添加SQLiteDatabase db = ourHelper.getWriteableDatabase,然后针对&#34; db&#34;
运行查询public boolean mod_app_symps(String l, String modfinalMed) {
SQLiteDatabase db = outHelper.getWriteableDatabase();
String[] modalation = new String[] { MOD_ID, MOD_MEDICINE };
Cursor c24;
boolean iscontain = false;
c24 = db.query(DATABASE_TABLE18, modalation, "modalation_previousss_id = ? AND modalation_medicine = ?",new String[] { l , modfinalMed } , null, null, null);
if(c24.getCount() > 0) {
iscontain = true;
}
return iscontain;
}