Python Forum

Full Version: How to use list (structure) in the SQL Select command?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
You can't just make up stuff and expect it to work. See "Retrieving data" at http://zetcode.com/db/sqlitepythontutorial/
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.