Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to make a set in an sqlite3 table a variable on python?

Tags:

python

sqlite

This is my brief:

The teacher wants to use the results from students taking these quizzes to log their performance. The system should store the last three scores for each student. The teacher would like to be able to output the results of the quiz for a particular class, sorted:

  • in alphabetical order with each student’s highest score for the tests
  • by the highest score, highest to lowest
  • by the average score, highest to lowest.

Analyse the requirements in detail for this program and design, code, test and evaluate a program that will allow the teacher to select which class group to look at and which field to use when sorting the output data.

I thought I could do this but it doesn'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 it what it 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 its not that

like image 627
StaticFX Avatar asked Aug 08 '26 02:08

StaticFX


1 Answers

Your error has nothing to do with your question's title, nor with SQL. You try to use variables (Name, Score2 and Score3) that are not defined, so Python raises a NameError exception. You have to define these variables before using them.

This set aside, there are quite a few other issues with your script, the first and main one being with the update query: if you don't specify a where clause it will update all the existing records in your table. In a relational db schema, each table must have a primary key (a field or combination of fields that uniquely and unambiguously identify one single row). If you don't have one, you have to create it, and then you need to use it in your where clause if you want to update only one give row.

like image 66
bruno desthuilliers Avatar answered Aug 10 '26 15:08

bruno desthuilliers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!