Android SQLite查询不起作用

时间:2011-08-12 02:25:13

标签: android sql sqlite cursor

为方便起见,我构建了这个DBmanager类,但是getAllRows()方法引起了关于nullpointerexceptions的大惊小怪。我只是想用它来获取所有行而不进行任何过滤。我做错了什么?

    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.
        }

    }

}

3 个答案:

答案 0 :(得分:2)

嗯,'关于nullpointerexceptions的大惊小怪'几乎不是对你的问题的详细描述。发布堆栈跟踪会有所帮助。在您的情况下,您没有初始化db变量。 您需要致电getWritableDatabase()才能这样做。

答案 1 :(得分:1)

空指针异常是这里的线索。 db是否已实例化?或者就此而言已经初始化了?

答案 2 :(得分:0)

这可能是件小事,但我注意到private final String TABLE_ROW_ID = "id",你的ID不是以下划线开头的。我很确定Android需要_id。只是我的两分钱。