def update_reviews():
    inputs = list(request.form.values())
    key = [inputs[0]]
    data, _ = sql_command('SELECT * from reviews WHERE rid = ?', key)
    if len(data) == 0:
        command = '''INSERT INTO reviews VALUES (?,?,?,?,?)'''
        update_(command, inputs)
        print('data inserted!')
        flash('Data inserted', 'success')

    else:  # update row
        command = '''UPDATE reviews
                    SET rid = ?, bid = ?, uid = ?, stars = ?, text = ?
                    WHERE rid = ?;
                    '''
        update_(command, inputs+key)
        print('data updated!')
        flash('Data updated', 'success')

    return redirect(url_for('reviews'))
def update_checkins():
    inputs = list(request.form.values())
    key = [inputs[0]]
    data, _ = sql_command('SELECT * from checkins WHERE bid = ?', key)

    if len(data) == 0:
        command = '''INSERT INTO checkins VALUES (?,?,?,?,?,?,?,?)'''
        update_(command, inputs)
        print('data inserted!')
        flash('Data inserted', 'success')

    else:  # update row
        command = '''UPDATE checkins
                    SET bid = ?, Sunday = ?, Monday = ?, Tuesday = ?, Wednesday = ?, Thursday = ?, Friday = ?, Saturday = ?
                    WHERE bid = ?;
                    '''
        update_(command, inputs+key)
        print('data updated!')
        flash('Data updated', 'success')

    return redirect(url_for('checkins'))
def update_business():
    inputs = list(request.form.values())
    key = [inputs[0]]
    data, _ = sql_command('SELECT * from business WHERE bid = ?', key)

    if len(data) == 0:
        command = '''INSERT INTO business VALUES (?,?,?,?,?,?)'''
        update_(command, inputs)
        print('data inserted!')
        flash('Data inserted', 'success')

    else:  # update row
        command = '''UPDATE business
                    SET bid = ?, bname = ?, categories = ?, active = ?, review_count = ?, stars = ?
                    WHERE bid = ?;
                    '''
        update_(command, inputs+key)
        print('data updated!')
        flash('Data updated', 'success')

    return redirect(url_for('business'))
def delete_checkins(id):
    command = '''DELETE FROM checkins where bid = ?'''
    update_(command, [id])
    flash('Data Deleted', 'success')
    return redirect(url_for('checkins'))
def delete_business(id):
    command = '''DELETE FROM business where bid = ?'''
    update_(command, [id])
    flash('Data Deleted', 'success')
    return redirect(url_for('business'))
def delete_user(id):
    command = '''DELETE FROM users where uid = ?'''
    update_(command, [id])
    flash('Data Deleted', 'success')
    return redirect(url_for('users'))
def delete_review(id):
    command = '''DELETE FROM reviews where rid = ?'''
    update_(command, [id])
    flash('Data Deleted', 'success')
    return redirect(url_for('reviews'))