update database directly from REST API using sqlalchemy and pandas

15 views
Skip to first unread message

黄祥

unread,
Feb 13, 2022, 3:26:59 AM2/13/22
to web2py-users
document/api.py
def test():
    import pandas as pd

    from sqlalchemy import create_engine

    data = {'Name': ['Alex', 'Ronald', 'Jane'], 'Age': [10, 18, 33] }
    df = pd.DataFrame(data)

    table = 'stifix'

    #engine = create_engine('sqlite://', echo = False)
    engine = create_engine('sqlite:///test.db', echo = False)

    df.to_sql(table, con = engine, if_exists = 'append')

    #engine.execute("SELECT * FROM %s" % table).fetchall()

    rows = engine.execute("SELECT * FROM %s" % table).fetchall()

    return rows

terminal
$ curl -X GET -i http://localhost:8000/document/api/test
HTTP/1.1 200 OK
X-Powered-By: web2py
Content-Type: text/html; charset=utf-8
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Expires: Sun, 13 Feb 2022 08:20:18 GMT
Pragma: no-cache
Date: Sun, 13 Feb 2022 08:20:18 GMT
Server: Rocket 1.2.6 Python/3.8.12
Transfer-Encoding: Chunked
Connection: keep-alive

curl: (56) Illegal or missing hexadecimal sequence in chunked-encoding

note
code is work well when in execute in pure python terminal (started from import pandas till print(rows) )

objective
can update database directly from REST API using sqlalchemy and pandas

question
how to do it using web2py way ?

thanks and best regards,
stifan
Reply all
Reply to author
Forward
0 new messages