title: Python looping to obtain different dataframes from a SQL database
body: <p>I'm trying to connect to an SQL database and, within a loop, create separate dataframes for each different instance of Id, containing all the data related to that Id. I've tried a number of ways, without any success so far. I'm pretty new to all of this, so I'm probably making some rookie mistakes.</p>
  <p>Attempt 1:</p>
  <pre><code>import pandas as pd
  import pyodbc
  
  conn = pyodbc.connect('Driver={SQL Server};'
                        'Server=Server_name;'
                        'Database=Database;'
                        'UID=Username;'
                        'PWD=password;'
                        'Trusted_Connection=yes;')
  
  Name = ['HR','ZA','PR','FW']
  
  for x in Name:
      
      SQL = '''
          SELECT *
          FROM Database
          WHERE Id = {x}'''.format(x = x)
      cursor = conn.cursor()
      cursor.execute(SQL)
      df = pd.read_sql_query(SQL)
  </code></pre>
  <p>On this code, I get an 'invalid column name' programming error on the first Name 'HL'.</p>
  <p>Attempt 2:</p>
  <pre><code>import pandas as pd
  import pyodbc
  
  conn = pyodbc.connect('Driver={SQL Server};'
                        'Server=Server_name;'
                        'Database=Database;'
                        'UID=Username;'
                        'PWD=password;'
                        'Trusted_Connection=yes;')
  
      SQL = '''
          SELECT *
          FROM Database
      
  conn.autocommit = True
  
  cursor.execute(SQL)
  
  for [Id] in cursor:
      df = pd.Dataframe(SQL,conn)
          
  </code></pre>
  <p>On this code, I get a 'ValueError: too many values to unpack (expected 1)' - on the for statement.</p>
  <p>I want to put a lot more code in the for loop so I need it to be set up to work through each Id. I hope that makes sense. Any guidance would be greatly appreciated. Thanks</p>
  <h2>UPDATE:</h2>
  <p>Thanks for all comments/answers. For some reason I just couldn't get it to work in either of the formats above so I took it back to where I started from now I understand how to include the syntax for the loop variable. The following now works:</p>
  <pre><code>import pandas as pd
  import pyodbc
  
  conn = pyodbc.connect('Driver={SQL Server};'
                            'Server=Server_name;'
                            'Database=Database;'
                            'UID=Username;'
                            'PWD=password;'
                            'Trusted_Connection=yes;')
      
  Name = ['HR','ZA','PR','FW']
  
  for x in Name:
  
      SQL = pd.read_sql_query(
      '''
      SELECT *
      FROM Database_table
      WHERE Id = '{x}'
      '''.format(x = x), conn)
  
      df = pd.DataFrame(SQL)
      
  </code></pre>