Python Forum
How to use list (structure) in the SQL Select command?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use list (structure) in the SQL Select command?
#1
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.
Reply
#2
You can't just make up stuff and expect it to work. See "Retrieving data" at http://zetcode.com/db/sqlitepythontutorial/
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Lightbulb IMAP select command Linh_lee 2 1,186 Aug-19-2022, 09:11 PM
Last Post: Linh_lee
  select Eof extension files based on text list of filenames with if condition RolanRoll 1 1,565 Apr-04-2022, 09:29 PM
Last Post: Larz60+
  deleting select items from a list Skaperen 13 4,692 Oct-11-2021, 01:02 AM
Last Post: Skaperen
  Passing List of Objects in Command Line Python usman 7 3,274 Sep-27-2020, 03:45 PM
Last Post: ndc85430
  Select the other of 2 items in a list Clunk_Head 7 3,768 Sep-01-2020, 05:27 PM
Last Post: Clunk_Head
  Select correct item from list for subprocess command pythonnewbie138 6 3,387 Jul-24-2020, 09:09 PM
Last Post: pythonnewbie138
  python if command for a list fid 3 2,049 May-08-2020, 11:52 AM
Last Post: fid
  List structure lost when multiplying Protonn 2 2,301 Apr-23-2020, 04:16 AM
Last Post: buran
  Transform list or set regardless of nesting structure blubb 2 2,002 Mar-10-2020, 07:17 PM
Last Post: ibreeden
  Add list item into executable command zhome888 2 2,167 Dec-19-2019, 12:23 AM
Last Post: zhome888

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020