我正在尝试在 android 中显示 SQLite 数据库的内容。当我不在 db2.select 上添加任何条件时,该代码有效。但是,当我向查询添加条件时,它会标记一个错误“数据库上从未调用过关闭”。
解决方案是什么?
提前致谢。
这是我的代码:
public class LogDisp extends Activity {
final ResultLog resultlog = new ResultLog(this);
String[] FROM = {_ID,Type,Date,Question,Percent,Avg };
String testtype="Verbal:: Sentence Correction" ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//String sqlstr="SELECT Date, Question, Avg, Percent FROM TABLE_NAME2 where Question>5";
String Orderby= Question+"DESC";
SQLiteDatabase db2 = resultlog.getReadableDatabase();
Cursor cursor = db2.query(TABLE_NAME2,FROM,null, null, null, null, Orderby);
//Cursor cursor =db2.rawQuery(sqlstr, null);
ScrollView sv = new ScrollView(this);
LinearLayout ll= new LinearLayout(this);
sv.addView(ll);
final TableLayout tv = new TableLayout(this);
ll.addView(tv);
while(cursor.moveToNext()){
String type=cursor.getString(1) ;
String date=cursor.getString(2);
String question=cursor.getString(3);
String percent=cursor.getString(4);
String avg=cursor.getString(5);
TableRow tr= new TableRow(this);
tv.addView(tr);
final String space=""+""+""+""+""+"";
TextView txtview= new TextView(this);
txtview.setText(date+space+question+space+percent+space+avg);
tr.addView(txtview);
}
this.setContentView(sv);
db2.close();
cursor.close();
}
}
这是我的 SQLite 助手类:
public class ResultLog extends SQLiteOpenHelper{
private static final String DATABASE_NAME2 = "LogofResult.db";
private static final int DATABASE_VERSION2 = 1;
public ResultLog(Context ctx) {
super(ctx, DATABASE_NAME2, null, DATABASE_VERSION2);
}
// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
+ TABLE_NAME2+ "(" +
_ID + " integer primary key autoincrement, " +
Type +" text not null,"+
Date + " integer not null,"+
Question + " integer not null,"+
Avg+" real not null,"+
Percent +" real not null);" ;
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME2);
onCreate(db);
}
}