ОШ 2018. Группа А. Краевая lvl3 / std.sql.main

ru en cn

с начала прошло: 2230 д. 15:25
страница обновлена: 28.12.2024 04:31

std.sql.main: test.py

import sqlite3
import sys
from json import dumps

ERR_OK = 0
ERR_WA = 1
ERR_PE = 2
ERR_UH = 3

with (
        sqlite3.connect(":memory:") as con,
        open("test.sql") as sql,
        open("test.log", "w") as log):
    try:
        with open("00.sql") as common_sql:
            con.executescript(common_sql.read())
    except OSError:
        pass

    con.executescript(sql.read())

    try:
        with open("query.sql") as fquery:
            query = fquery.read()
    except OSError:
        query = None

    try:
        with open("solve.sql", 'r') as fid: sql = fid.read()
    except:
        log.write(str(ERR_PE) + '\n')
        log.write('File with query couldn\'t be read!')
        exit(0)

    try:
        cur = con.cursor()
        cur.execute('pragma foreign_keys = on')
        if query is not None:
            cur.execute('pragma query_only = off')
            cur.executescript(sql)
            cur.execute(query)
        else:
            cur.execute('pragma query_only = on')
            cur.execute(sql)
    except (sqlite3.Warning, sqlite3.Error) as e:
        if type(e) in (sqlite3.ProgrammingError,
                       sqlite3.OperationalError,
                       sqlite3.Warning):
            log.write(str(ERR_PE) + '\n')
            log.write(str(e))
            exit(0)

        print(e, file = sys.stderr)
        exit(-1)

    log.write(str(ERR_OK) + '\n')
    ans = dumps(cur.fetchall())
    print(ans, file=log)
Дальневосточный федеральный университет