gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:29:38 24 4
gpt4 key购买 nike

这是我的数据库助手类:

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: FATAL EXCEPTION: main Process: com.dutt.rishabh.assignmentbuddy, PID: 27027 java.lang.NullPointerException at com.dutt.rishabh.assignmentbuddy.View$1.onClick(View.java:36) at android.view.View.performClick(View.java:4482) at android.view.View$PerformClick.run(View.java:18787) at android.os.Handler.handleCallback(Handler.java:808) at android.os.Handler.dispatchMessage(Handler.java:103) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:5345) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) at dalvik.system.NativeStart.main(Native Method)

最佳答案

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

在获取所有记录之前,您需要初始化dbHelper,如下所示:

dbHelper = new DatabaseHelper(this);

关于java - 如何查看数据库中的所有数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38282955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com