Is it possible to make a set in a sqlite3 table to a variable in python?
This is my summary:
The teacher wants to use the results from the students taking these quizzes to record their performance. the system should store the last three grades for each student. The teacher would like to be able to display the quiz results for a specific class, sorted:
- in alphabetical order, with each student having the highest score on the tests.
- by highest score, highest to lowest
- by average score, from highest to lowest.
Review the requirements for this program and design, copy, test, and evaluate a program that allows the teacher to choose which class group to view and which field to use when sorting the output.
I thought I could do it, but it won't work:
import sqlite3
new_db = sqlite3.connect('Quiz.db')
c = new_db.cursor()
print "Sort Alphabetically"
print ""
c.execute("SELECT * FROM Scores ORDER BY Name ")
new_db.commit()
for row in c:
print row
print ""
print "Sort By Score"
print ""
c.execute("SELECT * FROM Scores ORDER BY -Score3, Score2, Score1 ")
new_db.commit()
for row in c:
print row
NewScore = int(input('Enter Your Newest Score: ')
c.execute("UPDATE Scores SET Name=?, Score1=?, Score2=? Score3=?",
(Name, Score2, Score3, NewScore))
c.execute("SELECT * FROM Scores")
for row in c:
print row
new_db.commit()
new_db.close()
this is what he says:
c.execute("UPDATE Scores SET Name=?, Score1=?, Score2=? Score3=?",(Name, Score2, Score3, NewScore))
NameError: name 'Name' is not defined
The table has already been made so that there is no
source to share
Your error has nothing to do with the question title or SQL. You are trying to use variables ( Name
, Score2
and Score3
) that are not defined, which is why Python throws an exception NameError
. You must define these variables before using them.
In this case, there are quite a few problems with your script, the first and the main one with the query update
: if you do not specify a clause where
, it will update all existing records in the table. In a db relational schema, each table must have a primary key (a field or combination of fields that uniquely and unambiguously identify a single row). If you don't have it, you have to create it and then you need to use it in your proposal where
if you only want to update one row.
source to share