Reputation: 71
"InitDatabase()" is called "inserRecord()" but it shows exception every time and in insertRecord() i have also called closeDatabase() in which i have code to close DB(database).
In starting of program i enter records using insertRecord() which works fine but at run time when i used to insert the record it returns new row id but when i select * from table
then it will not showing that last inserted record
is it good to store cursor in hashmap further if needed in that app.
public SQLiteDatabase initDatabase(){
Log.v("XIG","creating or opening database.");
db_helper= new DatabaseHelper(context,dbname,1);
DB = context.openOrCreateDatabase(dbname,SQLiteDatabase.CREATE_IF_NECESSARY, null);
DB.setVersion(1);
DB.setLocale(Locale.getDefault());
DB.setLockingEnabled(true);
DB = db_helper.getWritableDatabase();
return DB;
}
public void insertRecord(String Table,String columnId[],String value[],String typeOfValue[]){
initDatabase();
Log.v("XIG","creating new record.");
ContentValues new_record = new ContentValues();
for(int i=0; i<columnId.length; i++){
if(typeOfValue[i].equals("INT")){
new_record.put(columnId[i],Integer.parseInt(value[i]));
}
else{
new_record.put(columnId[i],value[i]);
}
}
long rowid;
try{
DB.beginTransaction();
rowid = DB.insert(Table,null,new_record);
DB.setTransactionSuccessful();
}
finally{
DB.endTransaction();
}
Log.v("XIG","inserted rowid in "+Table+": "+rowid);
closeDatabase();
}
initDatabase() method shows exception :
07-12 13:15:32.415: E/SQLiteDatabase(13643): close() was never explicitly called on database '/data/data/com.app/databases/mydb3'
07-12 13:15:32.415: E/SQLiteDatabase(13643): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
Upvotes: 0
Views: 302
Reputation: 60721
Don't try to open and close your database on every update.
Open the database once when your application starts up, and don't ever bother closing it. Reuse a single database connection throughout; your code is far simpler this way, and you don't lose anything.
Upvotes: 0
Reputation: 2158
After DB.setTransactionSuccessful();
you have to commit it by using, DB.commit(); then definitely it will works.
You can Store cursor data into the List of MashMap by using code below
public ArrayList<HashMap<String, String>> select(
String tableName, String[] fields,
String[] selectionArgs) {
Cursor cursor = // get data by firing query
cursor.moveToFirst();
ArrayList<HashMap<String, String>> mapList = new ArrayList<HashMap<String,String>>();
for (int i = 0; i < cursor.getCount(); i++) {
cursor.moveToPosition(i);
HashMap<String, String> map = new HashMap<String, String>();
for (int j = 0; j < fields.length; j++) {
map.put(fields[j], cursor.getString(j));
}
mapList.add(map);
}
return mapList;
}
Accept this answer if it works.
Upvotes: 1
Reputation: 311055
The close should be inside the finally block. If there was an exception it won't happen.
Upvotes: 0
Reputation: 10190
the problem is probably not in this function. Your program opens the database somewhere and does not close it before this function is called, which tries to "open" the already-open database..
as your error log says
Application did not close the cursor or database object that was opened here
Upvotes: 0