Spaces:
Running
Running
| 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 == 4 : | |
| tab = txt.split(";;;\n") | |
| txt = "" | |
| c = 0 | |
| cursor.execute("INSERT INTO code (id, titre, enonce, code_trous, test) VALUES (?, ?, ?, ?, ?)", (id, tab[0].replace("\n",""), tab[1], tab[2], tab[3])) | |
| id += 1 | |
| conn.commit() | |
| conn.close() | |
| def return_exercise(titre): | |
| conn = sqlite3.connect("code.db") | |
| cursor = conn.cursor() | |
| cursor.execute("SELECT * FROM code WHERE titre = ?", (titre,)) | |
| rows = cursor.fetchall() | |
| conn.close() | |
| return rows[0] | |