简体   繁体   English

Python sqlite3值查找

[英]Python sqlite3 value lookup

I have a problem that I've created for myself as a challenge and I need your help. 我遇到的一个难题是我为自己创造的,需要您的帮助。

I have two tables. 我有两张桌子。 [students] - whose columns include name, course, score [gradingscheme] - whose columns include letter, upper, lower [学生]-其栏包括姓名,课程,分数[gradingscheme]-其栏包括字母,上,下

The [gradingscheme] table basically exists to serve as a lookup table so that if my score from [students] is between the upper and lower boundaries from [gradingscheme], it takes on the text value of the letter column. [gradingscheme]表基本上可以用作查找表,因此,如果[students]的得分在[gradingscheme]的上下边界之间,那么它将采用字母列的文本值。 (I am aware that you can simply do a CASE, WHEN, THEN to assign a letter grade, but I'm trying to expand my knowledge base) (我知道您可以简单地进行一次“案例,时间,然后”来指定字母等级,但是我正在尝试扩展我的知识库)

import sqlite3
conn = sqlite3.connect('example3.db')
c = conn.cursor()

c.execute('''                                   
UPDATE students                                     
SET LetterGrade = CASE
    WHEN score between 90 and 100 then 'A'
    WHEN score between 80 and 90 then 'B'
    ELSE 'C'
END
''')                                        

c.execute('''
SELECT s.name,s.courseid,c.name,c.classroom,s.score,s.LetterGrade
FROM students AS s
LEFT OUTER JOIN courses AS c
WHERE s.courseid=c.courseid
''')                                        

import pprint
pp=pprint.PrettyPrinter()
pp.pprint(c.fetchall())          

conn.commit()
conn.close()

This is what I have so far, and it's the UPDATE/SET part that I'm trying to replace in this challenge. 到目前为止,这就是我要做的,这是我要在此挑战中替换的UPDATE / SET部分。 I have no idea how to execute this. 我不知道如何执行此操作。 Any help is appreciated! 任何帮助表示赞赏!

  1. Your grading scheme is ambiguous. 您的评分方案不明确。 Eg SELECT 90 BETWEEN 90 AND 100; 例如在SELECT 90 BETWEEN 90 AND 100; evaluates to true just as SELECT 90 BETWEEN 80 AND 90; 就像SELECT 90 BETWEEN 80 AND 90;一样,计算结果为true SELECT 90 BETWEEN 80 AND 90; does. 做。 You should probably rather use ranges like 90-100, 80 - 89, 70 - 79, ... 您可能应该使用90-100、80-89、70-79,...
  2. Do you really need to add the grading letter to the students table? 您是否真的需要在学生表中添加评分信? Usually, one would simply join the letter from the gradingscheme table whenever such a select is performed. 通常,只要执行了这样的选择,就可以简单地加入gradingscheme表中的字母。 Eg SELECT s.name, s.course, g.letter FROM students s, gradingscheme g WHERE s.score BETWEEN g.lower AND g.upper; 例如SELECT s.name, s.course, g.letter FROM students s, gradingscheme g WHERE s.score BETWEEN g.lower AND g.upper; . You could even create a view to have this done automatically. 您甚至可以创建一个视图来自动完成此操作。 This avoids redundancy in your data. 这样可以避免数据冗余。
  3. If you really want to add the letter grading explicitly to the students table, you could try something like UPDATE students SET letterGrade = (SELECT letter FROM gradingscheme WHERE score BETWEEN lower AND upper); 如果您确实想将字母分级显式添加到students表中,则可以尝试类似UPDATE students SET letterGrade = (SELECT letter FROM gradingscheme WHERE score BETWEEN lower AND upper); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM