Android 在sqlite表中始终返回null值
我成功地在数据库中插入了一条记录,但在显示插入的数据时,一列将始终返回null 这是我的数据库助手提供的Android 在sqlite表中始终返回null值,android,mysql,sql,sqlite,Android,Mysql,Sql,Sqlite,我成功地在数据库中插入了一条记录,但在显示插入的数据时,一列将始终返回null 这是我的数据库助手提供的 private static final String CREATE_TABLE_RECORD = "CREATE TABLE " + TABLE_RECORD + "(" + RECORD_ID + " INTEGER PRIMARY KEY," + SUBJECT_ID2 + " INTEGER," + STUDENT_ID2 +" INTEGER," +
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);
}
这是我的日志
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;
}
}
不包括typeOfExam参数,因此该值保持为null
像这样加上
Record sub = new Record(idStudent,idSubject, Integer.parseInt(scoreField.getText().toString()), totalScores,tv1.getText().toString(), createTime );
sub.setTypeOfExam(typeOfExam);//this line...
更新
您错过了
this.typeOfExam=type
在记录构造函数中您忘记将typeOfExam
初始化到记录中(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;
}
这就是为什么每次设置其他参数时,
typeOfExam
都会得到NULL
值的原因?它来自另一个活动。我只是接收其他参数的数据。我甚至尝试在保存时直接输入,但它仍然会给我一个null。tv1.getText()。toString()是typeofExam@user3374426但是,您在哪里设置了所有其他参数?它来自另一个活动。我只是接收其他参数的数据。我甚至尝试在保存时直接输入,但它仍然会给我一个空值。@Crusader谢谢你的帮助。我已经解决了我的问题。它在我的唱片课上。我忘了在构造函数中包含要实例化的typeOfexam。感谢you@user3374426很高兴能帮助你。。。快乐编码!!:)是的,我在检查我的唱片课时意识到了。。感谢you@user3374426欢迎你!只有愚蠢的错误才能用很长的时间来解决。
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;
}