我想就我的 android 代碼尋求一些幫助。我正在嘗試使用 SQLite 開發一個測驗應用程式。我的資料庫有兩個表。一份用于學生資訊,一份用于問答。學生姓名、學生 ID 等資訊是通過 textViews 輸入的。在結果活動中進行測驗后,分數會顯示出來。但我也希望將此分數保存在 Student_trable 的 COLUMN_SCORE 列中。
我嘗試使用此方法更新表:
`public static void addScore (int StudentScore){
ContentValues cv = new ContentValues();
cv.put(DataContract.StudentTable.COLUMN_SCORE, StudentScore);
Cursor c = db.rawQuery("SELECT * FROM student_info ORDER BY id DESC LIMIT 1 " ,null);
db.update(DataContract.StudentTable.TABLE_NAME1, cv, DataContract.StudentTable.COLUMN_SCORE "= ?", new String[] {String.valueOf (c)});
db.close();`
但我失敗了。請問有什么建議嗎?這是我的代碼的更多詳細資訊:
桌子:

除錯螢屏:

課程
`import android.provider.BaseColumns;
/** This Class is creating constant variables for the two tables of our database. */
public final class DataContract {
private DataContract(){}
public static class StudentTable implements BaseColumns {
public static final String TABLE_NAME1 = "student_info";
public static final String COLUMN_ID = "id";
public static final String COLUMN_AM = "studentAm";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_SEMESTER = "semester";
public static final String COLUMN_SCORE = "score";
public static final String COLUMN_DATE = "studentDate";
}`
/** 此類將 Quiz 資料庫與 SQLite 鏈接,更新方法在哪里*/
` public class DataDbHelper extends SQLiteOpenHelper {
/** The String stores the name of the database.*/
private static final String DATABASE_NAME = "Quiz.db";
/** The integer stores the version of the database*/
private static final int DATABASE_VERSION = 1;
private static SQLiteDatabase db;
private static volatile DataDbHelper instance = null;
/** This method creates the database.*/
public DataDbHelper (Context context) {
super (context, DATABASE_NAME, null, DATABASE_VERSION);
}
/** This method assign details to the columns.
* It calls commands and statements from SQLite using Strings.*/
@Override
public void onCreate (SQLiteDatabase db) {
/** Creates the table of questions.*/
this.db = db;
final String SQL_CREATE_QUESTIONS_TABLE = "CREATE TABLE "
QuestionTable.TABLE_NAME " ( "
QuestionTable._ID " INTEGER PRIMARY KEY AUTOINCREMENT,"
QuestionTable.COLUMN_QUESTION " TEXT,"
QuestionTable.COLUMN_IMAGE " TEXT,"
QuestionTable.COLUMN_ANSWER1 " TEXT,"
QuestionTable.COLUMN_ANSWER2 " TEXT,"
QuestionTable.COLUMN_ANSWER3 " TEXT,"
QuestionTable.COLUMN_ANSWER4 " TEXT,"
QuestionTable.COLUMN_ANSWERNUM " INTEGER "
")";
db.execSQL(SQL_CREATE_QUESTIONS_TABLE);
fillQuestionsTable();
/** Creates the table of students.*/
final String SQL_CREATE_STUDENT_TABLE = "CREATE TABLE "
StudentTable.TABLE_NAME1 " ( "
StudentTable.COLUMN_ID " INTEGER PRIMARY KEY AUTOINCREMENT,"
StudentTable.COLUMN_AM " TEXT,"
StudentTable.COLUMN_NAME " TEXT,"
StudentTable.COLUMN_SEMESTER " TEXT,"
StudentTable.COLUMN_SCORE " TEXT, "
StudentTable.COLUMN_DATE " TEXT "
")";
db.execSQL(SQL_CREATE_STUDENT_TABLE);
}
/** This method can be used to upgrade the tables with newer information.*/
@Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " QuestionTable.TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " StudentTable.TABLE_NAME1);
onCreate(db);
}
/** This method fill the Question table. */
private void fillQuestionsTable() {
Question q1 = new Question ("A is correct","arkas1", "Maria" ,"anna", "Nikos" , "Andy", 1);
addQuestion (q1);
Question q2 = new Question ("B is correct","arkas2", "kjbhkjhA" ,"Bkjgkgj", "Cjvjhgj" , null, 2);
addQuestion (q2);
Question q3 = new Question ("C is correct","arkas3", "cgxcvnx" ,"xcvnxvxv", "Cgnhxfgjxf" , null, 3);
addQuestion (q3);
Question q4 = new Question ("A is correct again",null, "fvjxfvA" ,"Bxfgxmg", "Cxgmxmx" , null, 1);
addQuestion (q4);
Question q5 = new Question ("D is correct",null, "Afghf" ,"fghxggggghB", "xfgxghxghC" , "fghxghxgh", 4);
addQuestion (q5);
}
/** This method is used to add new question from our sqlite database.*/
private void addQuestion(Question question){
ContentValues cv = new ContentValues();
cv.put(QuestionTable.COLUMN_QUESTION, question.getQuestion());
cv.put(QuestionTable.COLUMN_IMAGE, question.getImage());
cv.put(QuestionTable.COLUMN_ANSWER1, question.getAnswer1());
cv.put(QuestionTable.COLUMN_ANSWER2, question.getAnswer2());
cv.put(QuestionTable.COLUMN_ANSWER3, question.getAnswer3());
cv.put(QuestionTable.COLUMN_ANSWER4, question.getAnswer4());
cv.put(QuestionTable.COLUMN_ANSWERNUM, question.getAnswerNum());
db.insert(QuestionTable.TABLE_NAME, null, cv);
}
/** This method is used to add new student to our sqlite database.*/
public void addNewStudent(String studentAm, String studentName, String studentSemester, int studentScore, String studentDate){
/** on below line we are creating a variable for
// our sqlite database and calling writable method
// as we are writing data in our database.*/
SQLiteDatabase db = this.getWritableDatabase();
// on below line we are creating a
// variable for content values.
ContentValues values = new ContentValues();
// on below line we are passing all values
// along with its key and value pair.
values.put(StudentTable.COLUMN_AM, studentAm);
values.put(StudentTable.COLUMN_NAME, studentName);
values.put(StudentTable.COLUMN_SEMESTER, studentSemester);
values.put(StudentTable.COLUMN_SCORE, studentScore);
values.put(StudentTable.COLUMN_DATE, studentDate);
// after adding all values we are passing
// content values to our table.
db.insert(StudentTable.TABLE_NAME1, null, values);
// at last we are closing our
// database after adding database.
db.close();
}
/** we have created a new method for reading all the students from Student Table. */
public List<Student> getAllStudent() {
List<Student> StudentList = new ArrayList<>();
db = getReadableDatabase ();
// Choose Random Questions
Cursor c = db.rawQuery("SELECT * FROM student_info ",null );
if (c.moveToFirst()){
do {
Student student = new Student ();
student.setStudentAm(c.getString(c.getColumnIndexOrThrow(StudentTable.COLUMN_AM)));
student.setStudentName (c.getString(c.getColumnIndexOrThrow(StudentTable.COLUMN_NAME)));
student.setStudentSemester (c.getString(c.getColumnIndexOrThrow(StudentTable.COLUMN_SEMESTER)));
student.setStudentScore (c.getInt (c.getColumnIndexOrThrow(StudentTable.COLUMN_SCORE)));
student.setStudentDate (c.getString(c.getColumnIndexOrThrow(StudentTable.COLUMN_DATE)));
StudentList.add (student);
} while (c.moveToNext());
}
c.close ();
return StudentList;
}
/** we have created a new method for reading all the Questions from Question Table. */
public List<Question> getAllQuestions() {
List<Question> questionList = new ArrayList<>();
db = getReadableDatabase ();
/** Choose limit number of Random Questions */
Cursor c = db.rawQuery("SELECT * FROM quiz_question ORDER BY RANDOM() LIMIT 3 ",null );
if (c.moveToFirst()){
do {
Question question = new Question();
question.setQuestion(c.getString(c.getColumnIndexOrThrow(QuestionTable.COLUMN_QUESTION)));
question.setImage (c.getString(c.getColumnIndexOrThrow(QuestionTable.COLUMN_IMAGE)));
question.setAnswer1(c.getString(c.getColumnIndexOrThrow(QuestionTable.COLUMN_ANSWER1)));
question.setAnswer2(c.getString(c.getColumnIndexOrThrow(QuestionTable.COLUMN_ANSWER2)));
question.setAnswer3(c.getString(c.getColumnIndexOrThrow(QuestionTable.COLUMN_ANSWER3)));
question.setAnswer4(c.getString(c.getColumnIndexOrThrow(QuestionTable.COLUMN_ANSWER4)));
question.setAnswerNum (c.getInt(c.getColumnIndexOrThrow(QuestionTable.COLUMN_ANSWERNUM)));
questionList.add (question);
} while (c.moveToNext());
}
c.close ();
return questionList;
}
public static void addScore (int StudentScore){
ContentValues cv = new ContentValues();
cv.put(DataContract.StudentTable.COLUMN_SCORE, StudentScore);
Cursor c = db.rawQuery("SELECT * FROM student_info ORDER BY id DESC LIMIT 1 " ,null);
db.update(DataContract.StudentTable.TABLE_NAME1, cv, DataContract.StudentTable.COLUMN_SCORE "= ?", new String[] {String.valueOf (c)});
db.close();
`
uj5u.com熱心網友回復:
方法中應該有 2 個引數addScore():id學生的和要存盤在表中的新分數。
新分數必須保存在ContentValues物件中,id并將用作方法的最后一個引數update()。
此外,無需SELECT查詢。
用這個:
public static void addScore(int id, int studentScore) {
ContentValues cv = new ContentValues();
cv.put(DataContract.StudentTable.COLUMN_SCORE, studentScore);
db.update(
DataContract.StudentTable.TABLE_NAME1,
cv,
DataContract.StudentTable.COLUMN_ID " = ?",
new String[] {String.valueOf(id)}
);
db.close();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/407455.html
標籤:
上一篇:亂數生成器反復回傳0
