在Android中获取Sqlite数据库轮询异常

在Android中获取Sqlite数据库轮询异常,android,sqlite,Android,Sqlite,我正在做一个在线应用程序项目。我的应用程序工作正常,但当我试图从服务器获取数据并调用sqlite数据库函数时,有时应用程序会崩溃,并在logcat中显示数据库池异常。这种情况发生的时间很少。下面是我关于stackoverflow中android数据库池异常的回答。大多数答案是这样的:删除所有db.close。使用此方法后,现在我的应用程序崩溃减少了。但问题仍然存在。请检查我的数据库代码,如果发现任何问题,请告诉我。提前谢谢。对不起,英语不好 package com.example.helper;

我正在做一个在线应用程序项目。我的应用程序工作正常,但当我试图从服务器获取数据并调用sqlite数据库函数时,有时应用程序会崩溃,并在logcat中显示数据库池异常。这种情况发生的时间很少。下面是我关于stackoverflow中android数据库池异常的回答。大多数答案是这样的:删除所有db.close。使用此方法后,现在我的应用程序崩溃减少了。但问题仍然存在。请检查我的数据库代码,如果发现任何问题,请告诉我。提前谢谢。对不起,英语不好

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");
    }

}

请致电onDistroy的db.close。当我遇到这个数据库轮询异常时,我尝试了这个方法,它对我有效。试试看