Beispiel #1
0
def list_result(engine):
    engine = sa.create_engine(engine)
    result = {}
    with engine.begin() as connection:
        select = vote_results.select()
        results = connection.execute(select)
        for id, name, votes in results.fetchall():
            result[name] = votes
        result = json.dumps(result)
        return result
Beispiel #2
0
def increase_animal(engine, animal):
    engine = sa.create_engine(engine)
    with engine.begin() as connection:
        select = vote_results.select().where(vote_results.c.name == animal)
        results = connection.execute(select)
        id, _, votes = results.fetchone()

        new_votes = votes + 1
        update = (vote_results.update().values(votes=new_votes).where(
            vote_results.c.id == id))
        connection.execute(update)
Beispiel #3
0
def get_results_list():
    engine = sa.create_engine("sqlite:///vote_db.sqlite")
    with engine.begin() as connection:
        query = vote_results.select()
        results = connection.execute(query)

        res_list = []

        for _, name, votes in results:
            res_list.append([name, votes])

        return res_list
Beispiel #4
0
def make_vote(animal):
    engine = sa.create_engine("sqlite:///vote_db.sqlite")
    with engine.begin() as connection:
        select = vote_results.select().where(vote_results.c.name == animal)
        results = connection.execute(select)
        id, _, votes = results.fetchone()

        new_vote = votes + 1

        update = (vote_results.update().values(votes=new_vote).where(
            vote_results.c.id == id))
        connection.execute(update)
Beispiel #5
0
def get_results_json():
    engine = sa.create_engine("sqlite:///vote_db.sqlite")
    with engine.begin() as connection:
        query = vote_results.select()
        results = connection.execute(query)

        # res_list = []
        res_json = {}

        for _, name, votes in results:
            # res_list.append([name, votes])
            res_json[name] = votes

        return json.dumps(res_json)
Beispiel #6
0
def results():
    engine = sa.create_engine("sqlite:///my_db.sqlite")
    dict = {}
    with engine.begin() as connection:
        select = vote_results.select()
        results = connection.execute(select)

        for id, name, votes in results.fetchall():
            dict[name] = votes
        print(dict)
        return dict


# results()
Beispiel #7
0
def add_animal_vote(animal):
    """
    Функция для того чтобы принять название животного, найти его в базе и обновить значение (количество голосов)
    :param animal:
    :return:
    """

    # engine = sa.create_engine("sqlite:///scripts/my_db.sqlite")
    engine = sa.create_engine("sqlite:///my_db.sqlite")

    with engine.begin() as connection:
        select = vote_results.select().where(vote_results.c.name == animal)
        results = connection.execute(select)

        id, _, votes = results.fetchone()

        new_votes = votes + 1
        update = (vote_results.update().values(votes=new_votes).where(
            vote_results.c.id == id))
        connection.execute(update)
Beispiel #8
0
import sqlalchemy as sa

from app.db import vote_results

if __name__ == "__main__":
    engine = sa.create_engine("sqlite:///my_db.sqlite") 
    with engine.begin() as connection: 
        select = vote_results.select() 
        results = connection.execute(select)  
        for id, name, votes in results.fetchall():  
            print(id, name, votes)
Beispiel #9
0
import sqlalchemy as sa

from app.db import vote_results

if __name__ == "__main__":
    engine = sa.create_engine("sqlite:///my_db.sqlite")
    with engine.begin() as connection:
        select = vote_results.select().where(vote_results.c.name == "parrots")
        results = connection.execute(select)
        id, _, votes = results.fetchone()

        new_votes = votes + 1
        update = (vote_results.update().values(votes=new_votes).where(
            vote_results.c.id == id))
        connection.execute(update)
Beispiel #10
0
import sqlalchemy as sa

from app.db import vote_results

if __name__ == "__main__":
    engine = sa.create_engine("sqlite:///my_db.sqlite")
    with engine.begin() as connection:
        select = vote_results.select().where(vote_results.c.name == "dogs") 
        results = connection.execute(select)
        id, _, votes = results.fetchone()  

        new_votes = votes + 1
        update = ( 
            vote_results.update().values(votes=new_votes).where(vote_results.c.id == id)
        )
        connection.execute(update)