2011-08-12 122 views
0

爲了方便,我構建了這個DBmanager類,但getAllRows()方法正在引起關於nullpointerexceptions的大驚小怪。我只是想用它來獲取所有行而不進行任何過濾。我究竟做錯了什麼?Android SQLite查詢不起作用

package com.com.com; 

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 DBManager { 

    private SQLiteDatabase db; // a reference to the database manager class. 
    private final String DB_NAME = "calls.db"; // the name of our database 
    private final int DB_VERSION = 1; // the version of the database 

    // the names for our database columns 
    private final String TABLE_NAME = "calls"; 
    private final String TABLE_ROW_ID = "id"; 
    public final String TABLE_ROW_ONE = "number"; 
    public final String TABLE_ROW_TWO = "date"; 

    public void addRow(String rowStringOne, String rowStringTwo){ 
     // this is a key value pair holder used by android's SQLite functions 
     ContentValues values = new ContentValues(); 

     // this is how you add a value to a ContentValues object 
     // we are passing in a key string and a value string each time 
     values.put(TABLE_ROW_ONE, rowStringOne); 
     values.put(TABLE_ROW_TWO, rowStringTwo); 

     // ask the database object to insert the new data 
     try 
     { 
      db.insert(TABLE_NAME, null, values); 
     } 
     catch(Exception e) 
     { 
      Log.e("DB ERROR", e.toString()); // prints the error message to the log 
      e.printStackTrace(); // prints the stack trace to the log 
     } 
    } 

    public Cursor getAllRows(){ 
     Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null, null); 
     //Cursor cursor = db.query(TABLE_NAME, new String[] { TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO }, null, null, null, null, null); 
     return cursor; 
    } 

    /* 
    * SQLiteHelper Class 
    */ 
    private class CustomHelper extends SQLiteOpenHelper{ 

     public CustomHelper(Context context){ 
      super(context, DB_NAME, null, DB_VERSION); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db){ 
      // the SQLite query string that will create our 3 column database table. 
      String newTableQueryString =  
        "create table " + 
          TABLE_NAME + 
          " (" + 
          TABLE_ROW_ID + " integer primary key autoincrement not null," + 
          TABLE_ROW_ONE + " text," + 
          TABLE_ROW_TWO + " text" + 
          ");"; 

      // execute the query string to the database. 
      db.execSQL(newTableQueryString); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){ 
      // NOTHING TO DO HERE. THIS IS THE ORIGINAL DATABASE VERSION. 
      // OTHERWISE, YOU WOULD SPECIFIY HOW TO UPGRADE THE DATABASE 
      // FROM OLDER VERSIONS. 
     } 

    } 

} 

回答

2

那麼,'關於nullpointerexceptions的大驚小怪'並不是你的問題的詳細描述。發佈堆棧跟蹤會有所幫助。在你的情況下,你並沒有初始化你的變量db。 您需要撥打getWritableDatabase()才能這樣做。

+0

對不起,沒有更詳細的描述。事實上你是對的,db還沒有初始化。謝謝! – fred

1

空指針異常是這裏的線索。是db實例化?或者爲此而初始化?

+0

不,它沒有。謝謝。 – fred

0

這可能是一件小事,但我注意到,在private final String TABLE_ROW_ID = "id",您的ID不以下劃線開頭。我很確定Android需要這個版本是_id。只是我的兩分錢。