如何查看数据库中的所有数据?

时间:2016-07-09 14:42:00

标签: java android android-studio android-sqlite sqliteopenhelper

这是我的数据库助手类:

public class DatabaseHelper extends SQLiteOpenHelper {
  public static final String DATABASE_NAME="assignment.db";
  public static final String TABLE_NAME="assignment_data";
  public static final int DATABASE_VERSION=1;
  public static final String COL_1="ID";
  public static final String COL_2="ASSIGNMENT_NAME";
  public static final String COL_3="SUBMISSION_DATE";
  public static final String COL_4="INFORMATION";
  public static final String COL_5="REMARKS";

  public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
    String create="create table "+TABLE_NAME+"("+COL_1+" INTEGER PRIMARY KEY AUTOINCREMENT,"+COL_2+" TEXT NOT NULL,"+COL_3+" TEXT NOT NULL,"+COL_4+" TEXT NOT NULL,"+COL_5+" TEXT NOT NULL"+")";
    db.execSQL(create);
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int i, int i1) {
    String upgrade="Drop table if exists "+TABLE_NAME;
    db.execSQL(upgrade);
    onCreate(db);
  }

  public boolean addAssignment(Assignment x){
    SQLiteDatabase myDB=this.getWritableDatabase();
    ContentValues content=new ContentValues();
    content.put(COL_2,x.getName());
    content.put(COL_3,x.getDate());
    content.put(COL_4,x.getInfo());
    content.put(COL_5,x.getRemarks());
    long result=myDB.insert(TABLE_NAME,null,content);
    if(result == -1){
      return false;
    }
    else {
      return true;
    }
  }

  public Cursor viewAll(){
    SQLiteDatabase MyDB=this.getReadableDatabase();
    Cursor res=myDB.rawQuery("SELECT * FROM "+DatabaseHelper.TABLE_NAME,null);
    return res;
  }

  /*public Cursor view(){
    Cursor cursor=myDB.rawQuery("SELECT * FROM "+TABLE_NAME+" WHERE ID=?",new String[]{COL_1+""});
    return cursor;
  }*/
}

这是我的View类:

public class View extends Activity {
  Button viewAll,show;
  EditText id;
  DatabaseHelper dbHelper;
  int id1;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view);
    viewAll = (Button)findViewById(R.id.viewAll);
    show = (Button)findViewById(R.id.show);
    id = (EditText)findViewById(R.id.id);
    loadData();
    //showData();
  }

  public void loadData(){
    viewAll.setOnClickListener(new android.view.View.OnClickListener() {
      @Override
      public void onClick(android.view.View view) {
        Cursor cursor = dbHelper.viewAll();
        if(cursor.getCount() == 0){
          Toast.makeText(getApplicationContext(),"No Data for any Assignment found!",Toast.LENGTH_SHORT).show();
        }
        StringBuffer s = new StringBuffer("");
        while(cursor.moveToNext()){
          s.append("ID:"+cursor.getString(0)+"\n");
          s.append("ASSIGNMENT NAME:"+cursor.getString(1)+"\n");
          s.append("SUBMISSION DATE:"+cursor.getString(2)+"\n");
          s.append("INFORMATION:"+cursor.getString(3)+"\n");
          s.append("REMARKS:"+cursor.getString(4)+"\n\n");
        }
        showMessage("ASSIGNMENT DETAILS:",s.toString());
      }
    });
  }

  /*public void showData(){
    show.setOnClickListener(new android.view.View.OnClickListener() {
        @Override
        public void onClick(android.view.View view) {
            id1=Integer.parseInt(id.getText().toString());
            Cursor m=dbHelper.view(id1);
            if(m.getCount()==0){
                Toast.makeText(View.this,"No Data for the Assignment found!",Toast.LENGTH_SHORT).show();
            }
            StringBuffer sb= new StringBuffer();
            while(m.moveToNext()){
                sb.append("ID:"+m.getString(0)+"\n");
                sb.append("ASSIGNMENT NAME:"+m.getString(1)+"\n");
                sb.append("SUBMISSION DATE:"+m.getString(2)+"\n");
                sb.append("INFORMATION:"+m.getString(3)+"\n");
                sb.append("REMARKS:"+m.getString(4)+"\n\n");
            }
            showMessage("ASSIGNMENT DETAILS:",sb.toString());
        }
    });

  }*/

  public void showMessage(String title,String  mess){
    AlertDialog.Builder x=new AlertDialog.Builder(View.this);
    x.setCancelable(true);
    x.setTitle(title);
    x.setMessage(mess);
    x.show();
  }
}

这是我得到的错误:

  

07-10 08:51:45.497 27027-27027 / com.dutt.rishabh.assignmentbuddy E / AndroidRuntime:致命异常:主要                                                                                         过程:com.dutt.rishabh.assignmentbuddy,PID:27027                                                                                         显示java.lang.NullPointerException                                                                                             在com.dutt.rishabh.assignmentbuddy.View $ 1.onClick(View.java:36)                                                                                             在android.view.View.performClick(View.java:4482)                                                                                             在android.view.View $ PerformClick.run(View.java:18787)                                                                                             在android.os.Handler.handleCallback(Handler.java:808)                                                                                             在android.os.Handler.dispatchMessage(Handler.java:103)                                                                                             在android.os.Looper.loop(Looper.java:193)                                                                                             在android.app.ActivityThread.main(ActivityThread.java:5345)                                                                                             at java.lang.reflect.Method.invokeNative(Native Method)                                                                                             在java.lang.reflect.Method.invoke(Method.java:515)                                                                                             在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:828)                                                                                             在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)                                                                                             在dalvik.system.NativeStart.main(本地方法)

3 个答案:

答案 0 :(得分:2)

我没有看到您的DatabaseHelper实例(dbHelper)在任何地方初始化。

在获取所有类似的记录之前,您需要初始化dbHelper

dbHelper = new DatabaseHelper(this);

答案 1 :(得分:0)

在进入while循环之前的onClick方法中添加以下行:

cursor.moveToFirst();

然后通读光标。如果这不能解决您的问题,请上传整个LogCat消息

答案 2 :(得分:0)

cursor.moveToPrevious()

之前致电while(cursor.moveToNext())