sqlite python opdracht af
This commit is contained in:
parent
56d10060c6
commit
27d906721c
32
Weekopdrachten/Week 3/Les 1/numers.py
Normal file
32
Weekopdrachten/Week 3/Les 1/numers.py
Normal file
@ -0,0 +1,32 @@
|
||||
import sqlite3
|
||||
import random
|
||||
import os
|
||||
|
||||
if os.path.exists('db.db'):
|
||||
os.remove('db.db')
|
||||
|
||||
con = sqlite3.connect('db.db')
|
||||
cur = con.cursor()
|
||||
|
||||
cur.execute(''' SELECT count(name) FROM sqlite_master WHERE type='table' AND name='cijfers' ''') # tabel cijfers opvragen
|
||||
|
||||
if cur.fetchone()[0] == 0: # als tabel niet bestaat
|
||||
cur.execute(''' CREATE TABLE cijfers (cijfer INTEGER) ''') # maak tabel aan met 1 kolom
|
||||
for x in range(1000): # 1000 keer loopen
|
||||
cijfer = (random.randrange(1, 500)) # cijfer genereren tussen 1 en 500
|
||||
cur.execute(''' INSERT INTO cijfers VALUES(?) ''', (str(cijfer), )) # insert cijfer in tabel
|
||||
con.commit() # commit alle wijzigingen
|
||||
|
||||
cur.execute('''SELECT sum(cijfer) FROM cijfers''') # vraag de totaal waarde van de cijfer kolop
|
||||
print(f"Totaal waarde alle cijfers: {cur.fetchone()[0]}") # print de totaal waarde
|
||||
|
||||
cur.execute('''SELECT avg(cijfer) FROM cijfers''')
|
||||
print(f"Gemiddelde waarde alle cijfers: {cur.fetchone()[0]}")
|
||||
|
||||
cur.execute('''SELECT min(cijfer), max(cijfer) FROM cijfers''')
|
||||
minmax = cur.fetchone()
|
||||
print(f"Kleinste waarde: {minmax[0]}\nGrootste waarde: {minmax[1]}")
|
||||
|
||||
|
||||
con.commit()
|
||||
con.close()
|
Reference in New Issue
Block a user