Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用SQLite在python中打印有序分数_Python_Sqlite - Fatal编程技术网

如何使用SQLite在python中打印有序分数

如何使用SQLite在python中打印有序分数,python,sqlite,Python,Sqlite,我写这篇文章是因为我的python代码中出现了问题,这将是一个针对在校学生的测试,我无法添加一个工作解决方案,以允许将SQLite数据库的结果打印给用户,此外,我希望询问用户他们希望它的格式,例如字母顺序或分数升序/降序。请帮帮我 import random import sqlite3 as lite import os print ("Welcome to the Maths Test") Score = 0 question = 0 while True: Name = in

我写这篇文章是因为我的python代码中出现了问题,这将是一个针对在校学生的测试,我无法添加一个工作解决方案,以允许将SQLite数据库的结果打印给用户,此外,我希望询问用户他们希望它的格式,例如字母顺序或分数升序/降序。请帮帮我

import random
import sqlite3 as lite
import os

print ("Welcome to the Maths Test") 
Score = 0
question = 0

while True:
    Name = input("Please enter your name here: ")
    if Name.isdigit():
        print ("Do Not Enter a Number")
    elif Name.isalpha():
        break
while True:
    class_group = input("Please enter your class group number here: ")
    if class_group.isalpha():
        print ("Do not enter a number")
    elif class_group.isdigit():
        break       


while question < 10: 
    operation= random.randint(1,3)
    number1 = random.randint(1,25)
    number2 = random.randint(1,25)
    if operation == 1:
        print("What is", number1, "+", number2)
        answer = number1 + number2

    elif operation ==2:
        print("What is", number1, "*", number2)
        answer = number1 * number2

    else: 
        print("What is", number1, "-", number2)
        answer = number1 - number2

    while True:
        try:
            user_answer = int(input())
        except ValueError:
            print ("That is not a valid answer")
            continue 
        else:
            break

    if user_answer == answer: 
        print("Correct")
        Score += 1

    else:
        print("Incorrect")
    question += 1

    if question == 10:
        print("Well done you scored", Score, "/10")
        break
if os.path.isfile('results.db'):
    connection = lite.connect('results.db')
else:
    connection = lite.connect('results.db')

for x in range (1, 4):
    connection.execute('CREATE TABLE %s(Name INTEGER, Score TEXT);'% x)


def Pupil_Results(Name, Score):
    table = 'class%s' % class_group
    with connection:
        cur = connection.cursor()
        cur.execute('SELECT * FROM ' + table + ' WHERE name = "%s";' % Name)
        rows = cur.fetchall()

    if len(rows) > 0:
        cur.execute('UPDATE ' + table + ' SET Name = "%i" WHERE Score = "%s";' % (Name, Score))
    else:
        cur.execute('INSERT INTO ' + table + '(Name, Score) VALUES (?, ?);',           (Name, Score))   
Pupil_Results(Name, Score)
随机导入
将sqlite3导入为lite
导入操作系统
打印(“欢迎参加数学考试”)
分数=0
问题=0
尽管如此:
Name=输入(“请在此处输入您的姓名:”)
如果Name.isdigit():
打印(“不要输入数字”)
elif Name.isalpha():
打破
尽管如此:
class\u group=input(“请在此处输入您的班级组号:”)
如果class_group.isalpha():
打印(“不要输入数字”)
elif class_group.isdigit():
打破
当问题<10时:
运算=random.randint(1,3)
number1=random.randint(1,25)
number2=random.randint(1,25)
如果操作==1:
打印(“What is”,number1,“+”,number2)
答案=1号+2号
elif操作==2:
打印(“What is”,number1,“*”,number2)
答案=1号*2号
其他:
打印(“What is”,number1,“-”,number2)
答案=数字1-数字2
尽管如此:
尝试:
user\u answer=int(输入())
除值错误外:
打印(“这不是一个有效的答案”)
继续
其他:
打破
如果用户_answer==答案:
打印(“正确”)
分数+=1
其他:
打印(“不正确”)
问题+=1
如果问题==10:
打印(“你做得好”,得分,/10”)
打破
如果os.path.isfile('results.db'):
connection=lite.connect('results.db')
其他:
connection=lite.connect('results.db')
对于范围(1,4)内的x:
connection.execute('CREATE TABLE%s(Name INTEGER,Score TEXT);'%x)
def学生成绩(姓名、分数):
表='类%s'%class\u组
连接:
cur=connection.cursor()
cur.execute('SELECT*FROM'+table+'其中name=“%s””'%name)
rows=cur.fetchall()
如果len(行)>0:
当前执行('UPDATE'+table+'SET Name=“%i”WHERE Score=“%s”;”%(Name,Score))
其他:
当前执行('插入到'+表+'(名称,分数)值(?,);',(名称,分数))
学生成绩(姓名、分数)

使用SQLite的代码部分有几个问题

首先,无论您的
os.path.isfile('results.DB')
的结果如何,您都以相同的方式连接到DB,因此只需连接即可

其次,这一点更为重要,更为实际的做法是,只使用一个表,再添加一列
class_group
,而不是单独的表

第三,
name
应该是
TEXT
类型,
score
应该是
INTEGER
类型,而不是相反

第四,表可能已经存在,因此您应该将
如果不存在
添加到命令中

第五,您的函数应该将
class\u group
作为参数。它还应该将连接作为参数,或者建立连接本身

下面是该部分的外观:

def add_or_update_score(name, class_group, score):
   with lite.connect('results.db') as conn:
      # Create the table, if doesn't exist yet
      conn.execute('CREATE TABLE IF NOT EXISTS scores (name TEXT, class_group INTEGER, score INTEGER)')
      # Check if a record already exists
      if list(conn.execute('SELECT * FROM scores where name = ?', (name,))):
         # A record already exists
         conn.execute('UPDATE scores SET score = ? WHERE (name = ? AND class_group = ?)', (score, name, class_group))
      else:
         # No record exists
        conn.execute('INSERT INTO scores VALUES (?, ?, ?)', (name, class_group, score))

你能说得更具体些吗?您的问题到底是什么,代码中哪些是有效的,哪些是无效的?谢谢