Spaces:
Sleeping
Sleeping
File size: 1,099 Bytes
3152cc4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import sqlite3
def create_table():
conn = sqlite3.connect("code.db")
cursor = conn.cursor()
cursor.execute("DELETE FROM code;")
with open('code.txt', 'r') as f:
c = 0
txt = ""
id = 1
for l in f :
txt += l
if l == ";;;\n":
c += 1
if c == 3 :
tab = txt.split(";;;\n")
txt = ""
c = 0
cursor.execute("INSERT INTO code (id, titre, enonce, test)VALUES (?, ?, ?, ?)", (id, tab[0], tab[1], tab[2]))
id += 1
conn.commit()
conn.close()
def return_title():
conn = sqlite3.connect("code.db")
cursor = conn.cursor()
cursor.execute("SELECT id, titre FROM code")
rows = cursor.fetchall()
conn.close()
return [{"id" : v[0], "title" : v[1].replace("\n","")} for v in rows]
def return_exercise(id):
conn = sqlite3.connect("code.db")
cursor = conn.cursor()
cursor.execute("SELECT * FROM code WHERE id = ?", (id,))
rows = cursor.fetchall()
return rows[0]
create_table() |