Avoid colnames in executesql

124 views
Skip to first unread message

Jose

unread,
Dec 2, 2021, 7:15:46 AM12/2/21
to web2py-users
I'm migrating an old app that runs in an old version of web2py in python27. In a lot of places I use db.executesql with argument as_dict=True. The error that I am getting is that he asks me to define the colnames que es un nuevo argumento.

Sometimes SQL is a query to views and others to stored procedures


If I pass a list with the names of the fields it works, but it is something I want to avoid.

rows = db.executesql(_sql, as_dict=True, colnames=['MyField']) # This works fine

Is there any way to avoid the list with the names of the fields?

Jose

Jose

unread,
Dec 2, 2021, 9:15:40 AM12/2/21
to web2py-users
If I do not define colnames I get

rows[0]['MyField']
KeyError: 'MyField'

Jose

unread,
Dec 2, 2021, 9:30:37 AM12/2/21
to web2py-users

Something else if I make select * from myView I get

RuntimeError: Result set includes duplicate column names. Specify unique column names using the 'colnames' argument

but I do not have duplicate fields

Jose

unread,
Dec 2, 2021, 9:39:39 AM12/2/21
to web2py-users
I add something. If instead of making select * from MyView I doselect MyField, AnotherFiel from MyView

works in this way I create the dictionary with equal keys to the first letter of each field in lowercase

[{'m': 6, 'a': 'xxxxxx'}]

Jose

Jose

unread,
Dec 2, 2021, 12:45:19 PM12/2/21
to web2py-users
The problem is that I had not installed pyodbc (I use MS SqlServer) and I was using some version of the web2py itself.

Installing pyodbc works fine

Reply all
Reply to author
Forward
0 new messages