Home > front end >  python sql table with paramter to json
python sql table with paramter to json

Time:12-04

Good Day!

I am trying to conver sql query into json with python, but getting an error when try to use sql query with a paramater:

sql syntax error: incorrect syntax near "%" enter image description here

it works ok without setting paramater

My db is hana and module is hdbcli

my code

def db(db_name="xxx"):
    return dbapi.connect(address=db_name, port="xx", user="xx", password="123")

def query_db(query, args=(), one=False):
    cur = db().cursor()
    cur.execute(query, args)
    r = [dict((cur.description[i][0], value) for i, value in enumerate(row)) for row in cur.fetchall()]
    cur.connection.close()
    return (r[0] if r else None) if one else r


def test(request):
    my_query = query_db("select bname, name_text from addrs where num=%s", (100,))
    return JsonResponse(my_query, safe=False)

urlpatterns = [
    path('s4d/', test),
]

thanks

CodePudding user response:

hana with hdbdcli uses :placeholder for prepared statements

some mpre infrmation can be found

my_query = query_db("select bname, name_text from addrs where num=:num", {"num": 100})

you use for two parameter

where id=:id and c2= :c2

{"id": id, "c2": c2}
  • Related