32 lines
1.1 KiB
Python
32 lines
1.1 KiB
Python
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 name FROM sqlite_master WHERE type='table' AND name='hosts' ''') # 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() |