def order_csv(filename):
    try:
        conn = sqlite3.connect(current_app.config['DATABASE'], detect_types=sqlite3.PARSE_DECLTYPES)
        conn.execute('PRAGMA foreign_keys = ON;')
        csr = conn.cursor()
    except Exception as e:
        print('Error connecting to database: ', e)
        sys.exit()
    try:
        f = open(filename, newline='')
        r = csv.DictReader(f)
        print('\n csv file openned successfully')
    except Exception as e:
        print('Error opening csv file: ', e)
        sys.exit()
    for d_row in r:
        t_row = (int(d_row['ord_nbr']), d_row['prod_nbr'], int(d_row['ord_qty']), d_row['ord_date'])
        csr.execute('BEGIN TRANSACTION')
        try:
            v_sql = 'SELECT id FROM orders WHERE ord_nbr = ?'
            csr.execute(v_sql, (t_row[0],))
            t_id = csr.fetchone()
            if (t_id != None):
                print((('\nOrder number ' + str(t[0])) + ' already exists in orders table'))
                continue
            v_sql = 'SELECT id FROM products WHERE prod_nbr = ?'
            csr.execute(v_sql, (t_row[1],))
            t_pid = csr.fetchone()
            if (t_pid == None):
                print((('\nProduct number ' + str(t_row[1])) + ' does not exist in products table'))
                continue
            if ((t_id == None) and (t_pid != None)):
                v_sql = 'INSERT INTO orders (ord_nbr, ord_qty, ord_date, prod_id)\n                                       VALUES (?,?,?,?);'
                csr.execute(v_sql, (t_row[0], t_row[2], t_row[3], t_pid[0]))
                conn.commit()
        except Exception as e:
            print(('Error loading Orders table ' + str(e)))
            print('Order number: ', t_row[0])
            conn.rollback()
    f.close()
    conn.close()
def order_csv(filename):
    try:
        conn = sqlite3.connect(current_app.config['DATABASE'], detect_types=sqlite3.PARSE_DECLTYPES)
        conn.execute('PRAGMA foreign_keys = ON;')
        csr = conn.cursor()
    except Exception as e:
        print('Error connecting to database: ', e)
        sys.exit()
    try:
        f = open(filename, newline=)
        r = csv.DictReader(f)
        print('\n csv file openned successfully')
    except Exception as e:
        print('Error opening csv file: ', e)
        sys.exit()
    for d_row in r:
        t_row = (int(d_row['ord_nbr']), d_row['prod_nbr'], int(d_row['ord_qty']), d_row['ord_date'])
        csr.execute('BEGIN TRANSACTION')
        try:
            v_sql = 'SELECT id FROM orders WHERE ord_nbr = ?'
            csr.execute(v_sql, (t_row[0],))
            t_id = csr.fetchone()
            if (t_id != None):
                print((('\nOrder number ' + str(t[0])) + ' already exists in orders table'))
                continue
            v_sql = 'SELECT id FROM products WHERE prod_nbr = ?'
            csr.execute(v_sql, (t_row[1],))
            t_pid = csr.fetchone()
            if (t_pid == None):
                print((('\nProduct number ' + str(t_row[1])) + ' does not exist in products table'))
                continue
            if ((t_id == None) and (t_pid != None)):
                v_sql = 'INSERT INTO orders (ord_nbr, ord_qty, ord_date, prod_id)\n                                       VALUES (?,?,?,?);'
                csr.execute(v_sql, (t_row[0], t_row[2], t_row[3], t_pid[0]))
                conn.commit()
        except Exception as e:
            print(('Error loading Orders table ' + str(e)))
            print('Order number: ', t_row[0])
            conn.rollback()
    f.close()
    conn.close()