Python Forum
How to use list (structure) in the SQL Select command? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to use list (structure) in the SQL Select command? (/thread-9762.html)



How to use list (structure) in the SQL Select command? - pyuser1 - Apr-26-2018

Hello,

I have created a list of fields in the database. For example,
varlist = ['id','grade','age','fname','lname','projTitle'] 
How to use this list in the SQL command, instead of writing all the field names?

I tried 2 types of code:
 sqlcommand = ("select (varlist) from table1")
    cur.execute(sqlcommand) 
for fld in varlist:
        cur.execute("select fld from table1")
The error message I get is: pypyodbc.DatabaseError: ('07002', '[07002] [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.')
Thank you.


RE: How to use list (structure) in the SQL Select command? - woooee - Apr-26-2018

You can't just make up stuff and expect it to work. See "Retrieving data" at http://zetcode.com/db/sqlitepythontutorial/


RE: How to use list (structure) in the SQL Select command? - pyuser1 - Apr-27-2018

I didn’t “just make up stuff”. I wanted to give an example to explain what I needed to do. On the website you linked here, I couldn’t find the information on the problem that I was trying to solve. Anyway, I solved the problem very easy way.

My solution is: Create a string out of the list and add the string in the sql command.