# db-prawo-jazdy

### Based on [prawo-jazdy-resources](https://git.mandarynki.eu/maciej/prawo-jazdy-resources) by [maciej](https://git.mandarynki.eu/maciej), to better suit the needs of [nuxt-prawo-jazdy](https://git.mandarynki.eu/netman/nuxt-prawo-jazdy)

### Files summary

`main.py` is a script that reads the file `pytania_z_punktacja_11_2025.csv` (it may change, depending on the age of the file and its outdatedness, but also sometimes the lack of [point value](placeholder_for_blog_post) ==> (blog post about such shenanigans in the future)) and imports it/them into a SQLite database file

- `database.db` SQLite file filled with data from the file above
- `pytania_z_punktacja_11_2025.csv` is the `pytania_egzaminacyjne_na_kierowcę_2025.xlsx` file converted to CSV (string delimiter `;`) from the website of the Polish Ministry of Infrastructure [here](https://www.gov.pl/attachment/a6fac2fd-8e2d-415b-8e65-49f903f8e433) - now the new file displayed at the main [driver's license page]() doesn't contain the point value as opposed to the one provided above, [in that case a writing to the Ministry is needed, asking for the public information of that value for each question](placeholder_for_blog_post) ==> (blog post about such shenanigans in the future)

# Requirements

- python3
- tqdm

## Running

Just run `python main.py` IN THE SAME DIRECTORY AS THE SCRIPT IS LOCATED IN.

- If needed, modify the CSV file name inside the script AND SET THE ROWS ACCORDINGLY.
  - Also check if your CSV file has a header on its first row, the file provided in this repository does have one, hence the `next(reader)` command which skips the first row. If your script doesn't have such header, comment these lines out.
- If your CSV does not contain point values, contact the Ministry and get pairs of question IDs and their point values in CSV, then comment the lines below `## COMMENT OUT BELOW...` and above `## COMMENT OUT ABOVE`, and uncomment the lines below `## UNCOMMENT BELOW` and above `## UNCOMMENT ABOVE`
  - Set rows accordingly.
  - Also check if your point value CSV file has a header on its first row, the file provided in this repository does have one, hence the `next(reader)` command which skips the first row. If your script doesn't have such header, comment these lines out.

### Database structure

```sql
CREATE TABLE IF NOT EXISTS tasks (
    id INTEGER,
    correct_answer TEXT,
    media_url TEXT,
    weight INTEGER
);

CREATE TABLE IF NOT EXISTS questions (
    task_id INTEGER,
    lang TEXT,
    text TEXT
);
CREATE TABLE IF NOT EXISTS tasks_advanced (
    id INTEGER,
    correct_answer TEXT,
    media_url TEXT,
    weight INTEGER
);
CREATE TABLE IF NOT EXISTS questions_advanced (
    task_id INTEGER,
    lang TEXT,
    text TEXT,
    answer_a TEXT,
    answer_b TEXT,
    answer_c TEXT
);
CREATE TABLE IF NOT EXISTS categories (
    name TEXT,
    task_id INTEGER
);
```

## Criteria

Categories:

- AM
- A1
- A2
- A
- B1
- B
- C1
- C
- D1
- D
- T
- PT

Languages as such:

- PL
- EN
- DE
- UA

# Basic tasks query

According to the Ministry of Infrastructure, in an exam there are (basic):

- 4 questions for 1 point
- 6 questions for 2 points
- 10 questions for 3 points

Set below values accordingly to the requirements above or to your liking:

- NO_OF_QUESTIONS
- WEIGHT_OF_QUESTIONS
- LANG
- CATEGORY

```sql
SELECT
    tasks.id,
    tasks.correct_answer,
    tasks.media_url,
    tasks.weight,
    questions.text
FROM tasks
    LEFT JOIN questions ON tasks.id = questions.task_id
    LEFT JOIN categories ON tasks.id = categories.task_id
WHERE
    categories.name = CATEGORY AND
    questions.lang = LANG AND
    tasks.weight = WEIGHT_OF_QUESTIONS
ORDER BY random()
LIMIT (NO_OF_QUESTIONS);
```

# Advanced tasks query

According to the Ministry of Infrastructure, in an exam there are (advanced):

- 2 questions for 1 point
- 4 questions for 2 points
- 6 questions for 3 points

Set below values accordingly to the requirements above or to your liking:

- NO_OF_QUESTIONS
- WEIGHT_OF_QUESTIONS
- LANG
- CATEGORY

```sql
SELECT
    tasks_advanced.id,
    tasks_advanced.correct_answer,
    tasks_advanced.media_url,
    tasks_advanced.weight,
    questions_advanced.text,
    questions_advanced.answer_a,
    questions_advanced.answer_b,
    questions_advanced.answer_c
FROM tasks_advanced
    LEFT JOIN questions_advanced ON tasks_advanced.id = questions_advanced.task_id
    LEFT JOIN categories ON tasks_advanced.id = categories.task_id
WHERE
    categories.name = CATEGORY AND
    questions_advanced.lang = LANG AND
    tasks_advanced.weight = WEIGHT_OF_QUESTIONS
ORDER BY random()
LIMIT (NO_OF_QUESTIONS);
```
