I am working on an online application project. My application works fine, but once the application crashes and displays a database pool exception in logcat when I try to get data from the server and call the sqlite database function (not all the time, this takes very little time). There is my answer about the exception of the android database pool in stackoverflow. Most of the answers looked like "delete all db.close". After using this method, now I get less application crash. But the problem still exists. Please check my database code and let me know if you find any problems. Thank you in advance. Sorry for the bad english.
package com.example.helper; import java.util.HashMap; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class SQLiteHandler extends SQLiteOpenHelper { private static final String TAG = SQLiteHandler.class.getSimpleName(); // All Static variables // Database Version private static final int DATABASE_VERSION = 1; // Database Name private static final String DATABASE_NAME = "android_api"; // Login table name private static final String TABLE_LOGIN = "login"; // Login Table Columns names private static final String KEY_ID = "id"; private static final String KEY_NAME = "name"; private static final String KEY_EMAIL = "email"; private static final String KEY_CREATED_AT = "created_at"; public SQLiteHandler(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } // Creating Tables @Override public void onCreate(SQLiteDatabase db) { String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_EMAIL + " TEXT UNIQUE," + KEY_CREATED_AT + " TEXT" + ")"; db.execSQL(CREATE_LOGIN_TABLE); Log.d(TAG, "Database tables created"); } // Upgrading database @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // Drop older table if existed db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOGIN); // Create tables again onCreate(db); } /** * Storing user details in database * */ public void addUser(int uid,String name, String email) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_ID,uid); values.put(KEY_NAME, name); // Name values.put(KEY_EMAIL, email); // Email // Inserting Row long id = db.insert(TABLE_LOGIN, null, values); Log.d(TAG, "New user inserted into sqlite: " + id); } /** * Getting user data from database * */ public HashMap<String, String> getUserDetails() { HashMap<String, String> user = new HashMap<String, String>(); String selectQuery = "SELECT * FROM " + TABLE_LOGIN; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); // Move to first row cursor.moveToFirst(); if (cursor.getCount() > 0) { user.put("name", cursor.getString(1)); user.put("email", cursor.getString(2)); user.put("created_at", cursor.getString(3)); } cursor.close(); // return user Log.d(TAG, "Fetching user from Sqlite: " + user.toString()); return user; } public HashMap<String, Integer> getUid() { HashMap<String, Integer> uid = new HashMap<String,Integer>(); String selectQuery = "SELECT * FROM " + TABLE_LOGIN; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); // Move to first row cursor.moveToFirst(); Log.d("database", "before uid put"); if (cursor.getCount() > 0) { uid.put("uid",cursor.getInt(0)); } cursor.close(); // return user return uid; } /** * Re crate database Delete all tables and create them again * */ public void deleteUsers() { SQLiteDatabase db = this.getWritableDatabase(); // Delete All Rows db.delete(TABLE_LOGIN, null, null); Log.d(TAG, "Deleted all user info from sqlite"); } }
source share