2011-08-12 80 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。只是我的两分钱。