问题
In my application I use the sqlite Database
,When I run the application Logcat display me
close() was never explicitly called on database '/data/data/com.MAT.CanadaImmigrationApp/databases/CanadaianImmigrationApp'
E/Database(13822): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
I search on google and I found
db.close(); // to close your Database,
cur.close(); //to close your cursor
But I don't get the idea where I add this code?
DbHelper.class
public class DbHelper extends SQLiteOpenHelper {
// Database attributes
public static final String DB_NAME = "DB_NAME";
public static final int DB_VERSION = 1;
public static final String TABLE_QUESTIONS = "Questions_Answers";
public static final String COLUMN_CHAPTERS = "Chapters";
public static final String COLUMN_QUESTIONS = "Question";
public static final String COLUMN_CORRECT_ANSWER = "Correct_Answer";
public static final String COLUMN_OPT1 = "Answer1";
public static final String COLUMN_OPT2 = "Answer2";
public static final String COLUMN_OPT3 = "Answer3";
public DbHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "create table if not exists " + TABLE_QUESTIONS + " ( " + BaseColumns._ID + " integer primary key autoincrement, "
+ COLUMN_CHAPTERS + " text not null, "
+ COLUMN_QUESTIONS + " text not null, "
+ COLUMN_CORRECT_ANSWER + " text not null, "
+ COLUMN_OPT1 + " text not null, "
+ COLUMN_OPT2 + " text not null, "
+ COLUMN_OPT3 + " text not null);";
// Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+ TABLE_QUESTIONS);
onCreate(db);
}
}
Db_Adapter.class
public class Db_Adapter {
public static final String TABLE_QUESTIONS = "Questions_Answers";
public static final String COLUMN_CHAPTERS = "Chapters";
public static final String COLUMN_QUESTIONS = "Question";
public static final String COLUMN_CORRECT_ANSWER = "Correct_Answer";
public static final String COLUMN_OPT1 = "Answer1";
public static final String COLUMN_OPT2 = "Answer2";
public static final String COLUMN_OPT3 = "Answer3";
public static SQLiteDatabase databasesql;
private DbHelper Android_OpenDb_Helper;
private Context mycontext;
public static List<String> question_Set;
public static List<String> Answers_Set;
public static List<String> Option1;
public static List<String> Option2;
public static List<String> Option3;
public Db_Adapter( Context context)
{
this.mycontext =context;
}
public void close() {
Android_OpenDb_Helper.close();
}
public DbHelper createDatabase()
{
Android_OpenDb_Helper = new DbHelper(mycontext);
databasesql = Android_OpenDb_Helper.getWritableDatabase();
return Android_OpenDb_Helper;
}
public long addToDB(String chapters, String question,
String correctAnswer, String answer1, String answer2, String answer3)
{
return databasesql.insert(TABLE_QUESTIONS,null, createContentValues(chapters,question,correctAnswer,answer1,answer2,answer3));
}
private ContentValues createContentValues(String chapters, String question,
String correctAnswer, String answer1, String answer2, String answer3)
{
ContentValues myContentVal = new ContentValues();
myContentVal.put(COLUMN_CHAPTERS, chapters);
myContentVal.put(COLUMN_QUESTIONS, question);
myContentVal.put(COLUMN_CORRECT_ANSWER, correctAnswer);
myContentVal.put(COLUMN_OPT1, answer1);
myContentVal.put(COLUMN_OPT2, answer2);
myContentVal.put(COLUMN_OPT3, answer3);
return myContentVal;
}
public Cursor readData()
{
return databasesql.query(TABLE_QUESTIONS, new String[]{COLUMN_CHAPTERS,COLUMN_QUESTIONS,COLUMN_CORRECT_ANSWER,COLUMN_OPT1,COLUMN_OPT2,COLUMN_OPT3}, null, null, null, null, null);
}
public Cursor getQuestion()
{
return databasesql.query(TABLE_QUESTIONS, new String[]{COLUMN_QUESTIONS}, null, null, null, null, null);
}
public Cursor getQuizQuestions(int numQ)
{
return databasesql.rawQuery(
"select * from "+ TABLE_QUESTIONS +" ORDER BY RANDOM() LIMIT "
+ numQ, null);
}
public List<String> getQuestions(String difficulty) {
question_Set = new ArrayList<String>();
Answers_Set = new ArrayList<String>();
Option1= new ArrayList<String>();
Option2 = new ArrayList<String>();
Option3 = new ArrayList<String>();
Cursor c = databasesql.rawQuery(
"select * from "+TABLE_QUESTIONS+" where CHAPTERS = ?",
new String[] { difficulty });
while (c.moveToNext()) {
question_Set.add(c.getString(2).trim());
Answers_Set.add(c.getString(3).trim());
Option1.add(c.getString(4).trim());
Option2.add(c.getString(5).trim());
Option3.add(c.getString(6).trim());
}
return question_Set;
}
public int DropTable()
{
return databasesql.delete(TABLE_QUESTIONS, null, null);
}
}
And I am call this methods like:
For get question :
Cursor cursor = DB_Adapter.getQuestion();
For get insert :
DB_Adapter.addToDB(Chapters,Question,CorrectAnswer,Answer1,Answer2,Answer3);
Please let me know where should I add the db.close();
and cur.close();
Thanks.
回答1:
Try this scenario:
When ever you want to access with the database help function or fire any query then use to open Database.
After using that function or query, always close that database.
e.g:
db.open(); // function to open database for the use
db.YOUR_QUERY or ANY_FUNCTION
db.close();
Open and close of database is use to open databse in perticular mode like writable/readable and close it.
You can use below function in to databaseHelper class to Open and Close the database.
//---opens the database---
public DBAdapter2 open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
Hope it will help you.
Updated
change this:
question_Set.add(c.getString(2).trim());
Answers_Set.add(c.getString(3).trim());
Option1.add(c.getString(4).trim());
Option2.add(c.getString(5).trim());
Option3.add(c.getString(6).trim());
with below:
db.open();
question_Set.add(c.getString(2).trim());
db.close();
db.open();
Answers_Set.add(c.getString(3).trim());
db.close();
db.open();
Option1.add(c.getString(4).trim());
db.close();
db.open();
Option2.add(c.getString(5).trim());
db.close();
db.open();
Option3.add(c.getString(6).trim());
db.close();
回答2:
After calling the:-
DB_Adapter.addToDB(Chapters,Question,CorrectAnswer,Answer1,Answer2,Answer3);
**db.close();**
回答3:
There are two ways of closing the Cursor
and Database object.
First Way Close both the object after using it.(i.e whenever the process get completed that relate to your cursor)
Second Way
Add it in OnStop()
or onDestroy()
.
In OnStop()
or onDestroy()
Check whether the cursor and database object is null. If not then close it.
public void onStop(){
if(db!=null)
{db.close();}
if(cur!=null)
{ cur.close();}
}
It Should work.
回答4:
use this method to create data base
my constructor
public DBClass(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
context.openOrCreateDatabase(DATABASE_NAME, Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS prediction(country1 VARCHAR,Country2 VARCHAR,winner VARCHAR,pool VARCHAR);");
}
then in your database accessing functions use like these
public void databaseInsertOperations(String c1, String c2, String c3) {
db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(DBClass.COUNTRY_1, c1);
cv.put(DBClass.COUNTRY_2, c2);
cv.put(DBClass.WINNER, c3);
db.insert(DBClass.TABLE, null, cv);
db.close();
}
public ArrayList<String> databaseRetriveOperation() {
db = this.getWritableDatabase();
ArrayList<String> buffer = new ArrayList<String>();
cursor = db.rawQuery("select * from prediction", null);
if (cursor.getCount() == 0) {
System.out.println("Error No records found");
} else {
while (cursor.moveToNext()) {
buffer.add(cursor.getString(0) + "/" + cursor.getString(1)
+ "/" + cursor.getString(2));
}
}
db.close(); cursor.close();
return buffer;
}
worked out for me when i got this error
来源:https://stackoverflow.com/questions/13249129/close-the-cursor-and-db-when-use-the-sqlite-database