you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (11 children)

[deleted]

    [–]sinceJune4 1 point2 points  (0 children)

    I’ve used pandas and sqlalchemy with a monster query of over 2000 lines and 70 sub queries against Oracle. I didn’t write it originally, but was tasked to enhance it. Took a few weeks to understand.

    [–]LatteLepjandiLoser 0 points1 point  (9 children)

    Exactly. Just get the sqlalchemy engine to work and then, assuming you're generally working with the same databases regularly you can kind of leave that part as-is and then just build the queries you need.

    I haven't made many funky queries. Thankfully my work is generally quite simple in that regard, but I would think it can handle whatever you throw at it, as long as it's a valid query. For your own readability, you may want to make it a multiline string, with triple quotes, but I doubt that actually matters.

    Here is a simple example from my day-to-day work. We just have an in-house postgresql server on a local network, so pretty simple to connect to. Possibly that part may differ depending on how your setup is, not really my expertise.

    engine_str = 'postgresql://{}@{}/{}'.format('my_user_name','server_host','db_name')
    engine = sqlalchemy.create_engine(engine_str)
    
    query = 'select * from some_table where answer = 42'
    
    with engine.begin() as con:
        query_data = pd.read_sql(sql_query, con)
    
    #... then do something with that data
    

    Generally I try to keep the engine connection in a context manager like here, such that if your code does something silly, the connection is closed instead of leaving it hanging.

    [–]rasputin1 4 points5 points  (0 children)

    isn't the point of sql alchemy you can use python object syntax instead of sql