A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 潘天功 于 2013-3-17 21:41 编辑
  1. package com.itheima.note.db.dao;

  2. import java.util.ArrayList;
  3. import java.util.List;

  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.sqlite.SQLiteDatabase;

  7. import com.itheima.note.db.NoteSQLiteOpenHelper;
  8. import com.itheima.note.domain.NoteBean;

  9. //对数据库进行增删改查操作
  10. public class NoteDao {
  11.        //因为任何一个操作都需要得到NoteSQLiteOpenHelper hepler,所以把它放在构造方法中初始化
  12.       private NoteSQLiteOpenHelper helper;
  13.       public NoteDao(Context context){
  14.               helper = new NoteSQLiteOpenHelper(context);
  15.       }
  16.       //添加一条账目信息到数据库
  17.      public void add(String name,float money){
  18.            SQLiteDatabase db = helper.getWritableDatabase();
  19.                db.execSQL("insert into account (name,money) values (?,?)", new Object[]{name,money});
  20.                db.close();
  21.       }
  22.      //从数据库删除一条信息
  23.      public void delete(int id){
  24.              SQLiteDatabase db = helper.getWritableDatabase();
  25.              db.execSQL("delete from account where id=?", new Object[]{id});
  26.              db.close();
  27.      }
  28.      //修改数据库信息
  29.      public void update(int id,float newmoney){
  30.          SQLiteDatabase db = helper.getWritableDatabase();
  31.          db.execSQL("update account set money =? where id =?", new Object[]{newmoney,id});
  32.          db.close();
  33.      }
  34.      //查询数据库所有信息
  35.      public List<NoteBean>findAll(){
  36.              SQLiteDatabase db = helper.getWritableDatabase();
  37.              List<NoteBean> list = new ArrayList<NoteBean>();
  38.              //获取数据库查询的结果游标
  39.              Cursor cursor = db.rawQuery("select id,money,name from account ", null);
  40.              while(cursor.moveToNext()){
  41.                      int id = cursor.getInt(cursor.getColumnIndex("id"));
  42.                      String name = cursor.getString(cursor.getColumnIndex("name"));
  43.                      float money = cursor.getFloat(cursor.getColumnIndex("money"));
  44.                      NoteBean bean = new NoteBean(id, name, money);
  45.                      list.add(bean);
  46.                      bean = null;
  47.              }
  48.              db.close();
  49.                 return list;
  50.      
  51.      }
  52. }
  53. 这段代码 用以下测试类测试总是报空指针异常
  54. <div class="blockcode"><blockquote>package com.itheima.note.test;
  55. import com.itheima.note.db.dao.NoteDao;

  56. import android.test.AndroidTestCase;
  57. //对数据库操作进行测试
  58. public class TestNoteDao extends AndroidTestCase {
  59.         NoteDao dao = new NoteDao(getContext());
  60.     public void testAdd()throws Exception{
  61.             for(int i=0;i<10;i++){
  62.             dao.add("我是你"+i+"大爷", i);
  63.         }
  64.             }   
  65. }
复制代码

QQ截图20130303230400.png (12.31 KB, 下载次数: 24)

error

error

8 个回复

倒序浏览

回帖奖励 +5

没有学到,不会啊
回复 使用道具 举报

回帖奖励 +5

好复杂,不过值得研究一下
回复 使用道具 举报
我更看不懂
回复 使用道具 举报

回帖奖励 +5

配置文件,别忘了配置,你开启了调试功能
回复 使用道具 举报
彭波 发表于 2013-3-10 12:20
没有学到,不会啊

NoteDao dao = new NoteDao(getContext());
位置放错了、应该在testAdd()方法里进行初始化
回复 使用道具 举报
疯狂程序 发表于 2013-3-16 12:51
配置文件,别忘了配置,你开启了调试功能

NoteDao dao = new NoteDao(getContext());
位置放错了、应该在testAdd()方法里进行初始化
回复 使用道具 举报
清水 中级黑马 2013-6-19 18:11:08
8#

回帖奖励 +5

自问自答么 哈哈
回复 使用道具 举报
清水 发表于 2013-6-19 18:11
自问自答么 哈哈

都多少年前的事了 你也能翻出来  佩服啊
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马