我在数据库中成功插入了一条记录,但在显示插入的数据时,一列将始终返回null。
这来自我的数据库助手
private static final String CREATE_TABLE_RECORD = "CREATE TABLE "
+ TABLE_RECORD + "(" + RECORD_ID + " INTEGER PRIMARY KEY," + SUBJECT_ID2
+ " INTEGER," + STUDENT_ID2 +" INTEGER," + TYPE_EXAM + " TEXT," + SCORE + " INTEGER,"+ TOTAL_SCORE + " INTEGER," + CREATED_TIME
+ " DATETIME" + ")";
public long insertRecord(Record s) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(SUBJECT_ID2,s.getSubjectId());
values.put(STUDENT_ID2,s.getStudentId());
values.put(SCORE,s.getScore());
values.put(TYPE_EXAM,s.getTypeOfExam());
values.put(TOTAL_SCORE,s.getTotalScore());
values.put(CREATED_TIME,s.getCreatedTime());
// insert row
long id = db.insert(TABLE_RECORD, null, values);
Log.d("Score" ,String.valueOf(values));
return id;
}
this from my activity
public void saveRecord(View view){
Intent intent = new Intent(RecordScoreActivity.this, RecordActivity.class);
DatabaseHelper db = new DatabaseHelper(getApplicationContext());
Record sub = new Record(idStudent,idSubject, Integer.parseInt(scoreField.getText().toString()), totalScores,tv1.getText().toString(), createTime );
Bundle extras = new Bundle();
extras.putInt("id", idSubject);
extras.putString("type", typeOfExam);
extras.putInt("score", totalScores);
extras.putString("time", createTime);
intent.putExtras(extras);
long id = db.insertRecord(sub);
//int studentId, int subjectId, int recordId, int score,int totalScore, String type,String time
Log.d("Success","recordId = " + id);
db.close();
startActivity(intent);
}
这来自我的logcat
03-03 18:21:17.763: D/Score(2273): score=50 student_id2=1 created_time=2014-3-3 type_exam=null subject_id2=1 total_score=50
03-03 18:21:17.763: D/Success(2273): recordId = 12
我真的不知道为什么我为该列获取空值。
我的记录课
public class Record {
int studentId;
int subjectId;
int recordId;
int totalScore;
int score;
String typeOfExam;
String createdTime;
public Record(){
}
public Record(int studentId, int subjectId, int score,int totalScore, String type,String time){
this.studentId =studentId;
this.subjectId = subjectId;
//this.recordId= recordId;
this.score = score;
this.totalScore = totalScore;
this.createdTime = time;
}
public Record(String time){
this.createdTime = time;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public int getSubjectId() {
return subjectId;
}
public void setSubjectId(int subjectId) {
this.subjectId = subjectId;
}
public int getRecordId() {
return recordId;
}
public void setRecordId(int recordId) {
this.recordId = recordId;
}
public int getTotalScore() {
return totalScore;
}
public void setTotalScore(int totalScore) {
this.totalScore = totalScore;
}
public String getTypeOfExam() {
return typeOfExam;
}
public void setTypeOfExam(String typeOfExam) {
this.typeOfExam = typeOfExam;
}
public String getCreatedTime() {
return createdTime;
}
public void setCreatedTime(String createdTime) {
this.createdTime = createdTime;
}
}
答案 0 :(得分:1)
您忘记将typeOfExam
初始化为Record(int studentId, int subject....)
构造函数,如下所示:
public Record(int studentId, int subjectId, int score,int totalScore, String type,String time){
this.studentId =studentId;
this.subjectId = subjectId;
this.typeOfExam = type;
this.score = score;
this.totalScore = totalScore;
this.createdTime = time;
}
这就是为什么每次NULL
获得typeOfExam
值
答案 1 :(得分:0)
Record sub = new Record(idStudent,idSubject, Integer.parseInt(scoreField.getText().toString()), totalScores,tv1.getText().toString(), createTime );
不包含typeOfExam参数,因此该值保持为空。
像这样添加..
Record sub = new Record(idStudent,idSubject, Integer.parseInt(scoreField.getText().toString()), totalScores,tv1.getText().toString(), createTime );
sub.setTypeOfExam(typeOfExam);//this line...
<强>更新强>
你错过了记录构造函数中的this.typeOfExam = type;