如何查询学生表student中不同的班级class

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何查询学生表student中不同的班级class相关的知识,希望对你有一定的参考价值。

参考技术A 可以使用VLOOKUP。
VLOOKUP函数可以用来核对数据,多个表格之间快速导入数据。利用这个功能可按列查找,最终返回该列所需查询列序所对应的值。与之对应的HLOOKUP是按行查找的。

如何使用来自两个不同表SQLite的元素更新/删除

我正在制定学生成绩提交计划,该计划接受以下输入:学生证,学生名,学生姓,班级ID,班级姓名,成绩点和字母等级。为了防止数据冗余,我创建了三个不同的表。但是,当尝试“修改”或“删除”具有来自不同表的两个值的条目时,我遇到了问题。 (例如删除WHERE student.id等于STUDENT_TABLE中的student.id和WHERE class.id等于CLASS_TABLE中的class.id)

话虽如此,这是我目前用于修改学生班的修改代码。我需要以与student.id和class.id相关联的方式进行修改(它们位于两个不同的表中)

 modifyclass.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(studentid.getText().toString().trim().length()==0 || classid.getText().toString().trim().length()==0 || classname.getText().toString().trim().length()==0)
            {
                showMessage("Error", "Please enter Student & Class ID to update class. \n\nAll other Field Entries will be ignored");
                return;
            }
            Cursor c=db.rawQuery("SELECT * FROM CLASS_TABLE WHERE classid='"+classid.getText()+"'", null);
            if(c.moveToFirst())
            {
                db.execSQL("UPDATE CLASS_TABLE SET classid='"+classid.getText()+"',classname='"+classname.getText()+"' WHERE studentid='"+studentid.getText()+"' AND classid='"+classid.getText()+"'");
                showMessage("Success", "Class Record Modified");
            }
            else
            {
                showMessage("Error", "Invalid First and Last name or Class ID");
            }
            clearText();
        }
    });

编辑:

这是我的表格供参考:

db=openOrCreateDatabase("STUDENTGRADES", Context.MODE_PRIVATE, null);
    db.execSQL("CREATE TABLE IF NOT EXISTS STUDENT_TABLE (studentid TEXT, fname TEXT, lname TEXT)");
    db.execSQL("CREATE TABLE IF NOT EXISTS CLASS_TABLE(classid TEXT PRIMARY KEY, classname TEXT UNIQUE)");
    db.execSQL("CREATE TABLE IF NOT EXISTS GRADE_TABLE (studentid TEXT, classid TEXT, pointgrade INTEGER, lettergrade TEXT)");
答案

您永远不需要一次更新多个表,只需进行一次更改,例如:学生的姓名已更改(学生表将被更改)或者说成绩已更改(更改学生的成绩等级)。

因此,我认为最适合的方法是更新学生的详细信息(名字或姓氏或两者)以及改变成绩的方法(再一次或两者)。

至于删除,您可以从成绩表和学生表中顺序删除,反之亦然。

因此,我相信以下代码包括方法deleteStudentInfochangeStudentNamechangeStudentFirstNamechangeStudentLastNamechangeStudentGrade,还包括示例用法以及创建和填充表格(您可能希望考虑修订的架构): -

public class MainActivity extends AppCompatActivity {

    public static final String DBNAME = "study";
    public static final String STUDENT_TABLE_NAME = "STUDENT_TABLE";
    public static final String COL_STUDENT_ID = "studentid";
    public static final String COL_STUDENT_FIRSTNAME = "fname";
    public static final String COL_STUDENT_LASTNAME = "lname";

    public static final String CLASS_TABLE_NAME = "CLASS_TABLE";
    public static final String COL_CLASS_ID = "classid";
    public static final String COL_CLASS_NAME = "classname";

    public static final String GRADE_TABLE_NAME = "GRADE_TABLE";
    public static final String COL_GRADE_POINTGRADE = "pointgrade";
    public static final String COL_GRADE_LETTERGRADE = "lettergrade";
    public static final String BY_STUDENTID = COL_STUDENT_ID + "=?";
    public static final String BY_CLASSID = COL_CLASS_ID + "=?";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SQLiteDatabase db = openOrCreateDatabase(DBNAME,Context.MODE_PRIVATE,null);
        db.execSQL("CREATE TABLE IF NOT EXISTS " + STUDENT_TABLE_NAME + " (" +
                COL_STUDENT_ID + " TEXT PRIMARY KEY, " +
                COL_STUDENT_FIRSTNAME + " TEXT," +
                COL_STUDENT_LASTNAME + " TEXT)"
        );
        db.execSQL("CREATE TABLE IF NOT EXISTS " + CLASS_TABLE_NAME + "(" +
                 COL_CLASS_ID + " TEXT PRIMARY KEY," +
                 COL_CLASS_NAME + " TEXT UNIQUE " +
                ")"
        );
        db.execSQL("CREATE TABLE IF NOT EXISTS " + GRADE_TABLE_NAME + "(" +
                COL_STUDENT_ID + " TEXT, " +
                COL_CLASS_ID + " TEXT, " +
                COL_GRADE_POINTGRADE + " INTEGER, " +
                COL_GRADE_LETTERGRADE + " TEXT" +
                ")"
        );
        db.execSQL("INSERT OR IGNORE INTO " + STUDENT_TABLE_NAME +
                " VALUES" +
                "('00001','Fred','Smith')," +
                "('00010','Mary','Thomas')," +
                "('00910','Angela','Jones')"
        );
        db.execSQL("INSERT OR IGNORE INTO " + CLASS_TABLE_NAME +
                " VALUES" +
                "('001','English')," +
                "('101','Mathematics')," +
                "('201','Chemistry')"
        );
        db.execSQL("INSERT OR IGNORE INTO " + GRADE_TABLE_NAME +
                " VALUES" +
                "    ('00001','001',99,'A'), -- Fred Smith has 99 point grade as an A in English\n" +
                "    ('00001','101',25,'F'), -- Fred Smith has 25 point grade as an F on Mathematics\n" +
                "    ('00010','201',76,'B'), -- Angela Jones 76 a B in Chemistry\n" +
                "    ('00910','101',50,'C'), \n" +
                "    ('00910','201',63,'C'),\n" +
                "    ('00910','001',89,'A')\n" +
                ";"
        );

        changeStudentName(db,"00001","Joe","Bloggs");
        changeStudentFirstName(db,"00001","Harry");
        changeStudentLastName(db,"00001","Hoffmann");
        // e.g. won't change due to -1 (skip pointsgrade) and null (skip lettergrade)
        changeStudentGrade(db,"00001","001",-1,null);
        // Change both
        changeStudentGrade(db,"00001","001",25,"D");
        changeStudentGrade(db,"00001","001",27,null);

        // Ooops invalid student id
        if (deleteStudentInfo(db,"001")) {
            Log.d("DELETION","Student 001 deleted.");
        } else {
            Log.d("DELETION","Ooops Student 001 not deleted?????");
        }

        // Corrected Student ID
        if (deleteStudentInfo(db,"00001")) {
            Log.d("DELETION","Student 001 deleted.");
        } else {
            Log.d("DELETION","Ooops Student 001 not deleted?????");
        }
    }

    private boolean deleteStudentInfo(SQLiteDatabase db, String studentid) {

        String tag = "STUDENT_DELETE";
        String student_table = "STUDENT_TABLE";
        String grade_table = "GRADE_TABLE";

        long pre_delete_student_count = DatabaseUtils.queryNumEntries(db,student_table);
        long pre_delete_grade_count = DatabaseUtils.queryNumEntries(db,grade_table);

        String whereclause = "studentid =?";
        String[] whereargs = {studentid};

        db.delete(student_table,whereclause,whereargs);
        db.delete(grade_table,whereclause,whereargs);

        long post_delete_student_count = DatabaseUtils.queryNumEntries(db,student_table);
        long post_delete_grade_count = DatabaseUtils.queryNumEntries(db,grade_table);

        Log.d(
                tag,
                "Number of Students deleted from " +
                        student_table + " is " +
                        String.valueOf(
                                pre_delete_student_count - post_delete_student_count
                        ));
        Log.d(
                tag,
                "Number of Grades deleted from " + grade_table + " is " +
                        String.valueOf(
                                pre_delete_grade_count - post_delete_grade_count
                        )
        );
        if ((pre_delete_student_count + pre_delete_grade_count) != (post_delete_student_count + post_delete_grade_count)) {
            return true;
        }
        return false;
    }

    /**
     * Flexible Student Name Change
     *
     * @param db            The SQliteDatabase
     * @param studentid     The studentid (String)
     * @param newfirstname  The new firstname, null or blank to leave as is
     * @param newlastname   the new lastname, null or blank to leave as is
     */
    private void changeStudentName(SQLiteDatabase db, String studentid, String newfirstname, String newlastname ) {

        //Anything to do? if not do nothing
        if ((newfirstname == null || newfirstname.length() < 1) && (newlastname == null || newlastname.length() < 1)) {
            return;
        }
        ContentValues cv = new ContentValues();
        if (newfirstname != null && newfirstname.length() > 0) {
            cv.put(COL_STUDENT_FIRSTNAME,newfirstname);
        }
        if (newlastname != null && newlastname.length() > 0) {
            cv.put(COL_STUDENT_LASTNAME,newlastname);
        }
        // Overcautious check
        if (cv.size() < 1) {
            return;
        }
        db.update(STUDENT_TABLE_NAME,cv,BY_STUDENTID,new String[]{studentid});
    }

    /**
     * Change a Student's First Name (invokes changeStudentName method)
     * @param db            The SQLiteDatabase
     * @param studentid     The student's id (String)
     * @param newfirstname  The new first name to apply
     */
    private void changeStudentFirstName(SQLiteDatabase db, String studentid, String newfirstname) {
        changeStudentName(db,studentid,newfirstname,null);
    }

    /**
     * Change a Student's Last Name (invokes changeStudentName method)
     * @param db
     * @param studentid
     * @param newlastname
     */
    private void changeStudentLastName(SQLiteDatabase db, String studentid, String newlastname) {
        changeStudentName(db,studentid,null,newlastname);
    }

    /**
     * Change a students grade (allowing just one (points/letter))
     * @param db
     * @param studentid
     * @param classid
     * @param newpointsgrade
     * @param newlettergrade
     */
    private void changeStudentGrade(SQLiteDatabase db, String studentid, String classid, int newpointsgrade, String newlettergrade) {
        // Anything to do? if not do nothing
        if (newpoi

以上是关于如何查询学生表student中不同的班级class的主要内容,如果未能解决你的问题,请参考以下文章

查询每个班级排名第三的学生

查询每个班级排名第三的学生

查询每个班级排名第三的学生

如何使用来自两个不同表SQLite的元素更新/删除

表设计二,联接查询

建立一个存储过程student_info,要求根据班级查询学生的学号、姓名、课程号和分数(表结构如表2,表3)