import sqlite3
Class = str(input("Which class are you in? A) 11A B) 11B C) 11C"))
f_name = str(input("What is your first name?"))
s_name = str(input("What is your surname?"))
h_score = "8"
a_score = "7"
t_taken = "3"
if Class == "11A" or "11a":
counter = 0
while counter < 1:
db_students = sqlite3.connect('//persian/controlledassessment$/work/11 C CS/10BakeEd/Python/Task 2/db_Students.db')
c=db_students.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS ClassA
(student_firstname text,
student_surname text,
h_score number,
a_score number,
t_taken number,
Class text)
''')
c.execute('''INSERT INTO ClassA
VALUES (?, ?, ?, ?, ?, ?)''',(f_name, s_name, h_score, a_score, t_taken, Class))
db_students.commit()
c.execute("SELECT * FROM ClassA")
fetchall=c.fetchall()
print(fetchall)
db_students.close()
counter = counter + 1
elif Class == "11B" or "11b":
print()
elif Class == "11C" or "11c":
print()
else:
print("")
This is my code at the moment and some of the variables at the are placeholders. What I want to do is pull information out of the database that I have created and assign it to a variables so that I can use them in calculations such as averages.
fetchall()
returns a list of rows, where each row is a tuple containing (in this case) six values.
You could go through this list, but unless you need to do random accesses to all rows, it is a better idea to use the cursor as an iterator:
c.execute(...)
for row in c:
print(c[0]) # prints all first names
else:
pass # no rows found
If you want to compute averages, it would be a better idea to do this directly in SQL:
c.execute("SELECT AVG(h_score), AVG(a_score) FROM ClassA WHERE Class LIKE '11a'")
avg_h, avg_a = c.fetch()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.