Android Sample DB无法正常工作

时间:2012-10-01 03:01:19

标签: android database sqlite

我在博客上或在SO上发现了这个示例数据库代码(不记得了)

package com.learn.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBAdapter {

    private static final String DATABASE_NAME = " nba";
    private static final String DATABASE_TABLE = "bookList";
    private static final int DATABASE_VERSION = 1;
    private static String TAG = "Upgrading Database!";
    public static final String KEY_BOOK = "book";
    public static final String KEY_AUTHOR = "author";
    public static final String KEY_ISBN = "isbn";
    public static final String KEY_ROWID = "_id";
    public static final String KEY_RATING = "rating";
    public static final String KEY_STATUS = "status";

    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;

    private static final String DATABASE_CREATE = " create table  "
            + DATABASE_TABLE + " (" + KEY_ROWID
            + " integer primary key autoincrement,  " + KEY_AUTHOR
            + " text not null, " + KEY_BOOK + " text not null, " + KEY_RATING
            + " text not null, " + KEY_STATUS + " text not null, " + KEY_ISBN
            + " text not null); ";

    private final Context mCtx;

    public DBAdapter(Context ctx) {
        this.mCtx = ctx;

    }

    private static class DatabaseHelper extends SQLiteOpenHelper {
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);

        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(DATABASE_CREATE);

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("ALTER TABLE bookList ADD COLUMN String status");

        }
    }

    public DBAdapter open() throws SQLException {

        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        mDbHelper.close();
    }

    public long createBook(String book, String author, String isbn,
            float rating, String status) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_BOOK, book);
        initialValues.put(KEY_AUTHOR, author);
        initialValues.put(KEY_ISBN, isbn);
        initialValues.put(KEY_RATING, rating);
        initialValues.put(KEY_STATUS, status);

        return mDb.insert(DATABASE_TABLE, null, initialValues);

    }

    public boolean deleteBook(long rowId) {
        return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;

    }

    public Cursor fetchAllBooks() {
        return mDb.query(DATABASE_TABLE, new String[] { KEY_BOOK, KEY_ISBN,
                KEY_AUTHOR, KEY_ROWID, KEY_RATING, KEY_STATUS }, null, null,
                null, null, null);
    }

    public Cursor fetchBook(long rowId) throws SQLException {
        Cursor mCursor = mDb.query(DATABASE_TABLE, new String[] { KEY_BOOK,
                KEY_AUTHOR, KEY_ROWID, KEY_ISBN, KEY_RATING, KEY_STATUS },
                KEY_ROWID + "=" + rowId, null, null, null, null);

        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;

    }

    public boolean updateBook(long rowId, String book, String author,
            String isbn, float rating, String status) {
        ContentValues args = new ContentValues();
        args.put(KEY_BOOK, book);
        args.put(KEY_AUTHOR, author);
        args.put(KEY_ISBN, isbn);
        args.put(KEY_RATING, rating);
        args.put(KEY_STATUS, status);

        return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;

    }
}

我正在尝试在我的主要活动中添加一条记录,但是我在添加新书的行上遇到了logcat错误。

package com.learn.db;

import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Start extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start);
        DBAdapter fish_db = new DBAdapter(getApplicationContext());
        fish_db.createBook("Captain Underpants", "Dav Pilkey", "12343251321", 5, "In stock");
        Cursor cursor = fish_db.fetchAllBooks();
        if (cursor.moveToFirst()){
               do{
                  String data = cursor.getString(cursor.getColumnIndex("data"));
                    Toast.makeText(getApplicationContext(), data, Toast.LENGTH_LONG).show();
               }while(cursor.moveToNext());
            }
            cursor.close();
}
}

根据logcat的实际错误是行return mDb.insert(DATABASE_TABLE, null, initialValues);

上的空指针

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

mDb为空,您需要在fish_db.open()

之前先致电fish_db.createBook()