2015-05-27 39 views
5

我正在開發一個在線應用程序項目。我的應用程序正常工作,但有時應用程序崩潰,並顯示logcat數據庫池異常,當我試圖從服務器獲取數據並調用我的sqlite數據庫函數(並非所有的時間,它發生了很少的時間)。有關於stackoverflow中android數據庫池異常的答案。大部分答案都是這樣的「刪除所有db.close」。現在使用這種方法後,我的應用崩潰了。但問題仍然存在。請檢查我的數據庫代碼,並讓我知道如果你發現任何問題。提前致謝。對不起,英文不好。在Android中獲取Sqlite數據庫調查異常

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

} 

回答

1

請致電onDistroy db.close。當我面對這個數據庫輪詢異常時,我嘗試了這種方法,它對我很有用。嘗試一下。

相關問題