title: How to conduct SQL queries on multiple .db files and store the results in a .csv?
body: <p>I have about 100 .db files stored on my Google Drive which I want to run the same SQL query on. I'd like to store these query results in a single .csv file.</p>
  
  <p>I've managed to use the following code to write the results of a single SQL query into a .csv file, but I am unable to make it work for multiple files.</p>
  
  <pre><code>conn = sqlite3.connect('/content/drive/My Drive/Data/month_2014_01.db')
  
  df = pd.read_sql_query("SELECT * FROM messages INNER JOIN users ON messages.id = users.id WHERE text LIKE '%house%'", conn)
  
  df.to_csv('/content/drive/My Drive/Data/Query_Results.csv')
  </code></pre>
  
  <p>This is the code that I have used so far to try and make it work for all files, based on <a href="https://stackoverflow.com/questions/11850762/how-to-connect-to-multiple-sqlite3-database-with-python" title="How To Connect To Multiple sqlite3 Database with Python">this post</a>. </p>
  
  <pre><code>databases = []
  
  directory = '/content/drive/My Drive/Data/'
  for filename in os.listdir(directory):
      flname = os.path.join(directory, filename)
      databases.append(flname)
  
  for database in databases:
      try:
          with sqlite3.connect(database) as conn:
  
              conn.text_factory = str
              cur = conn.cursor()
              cur.execute(row["SELECT * FROM messages INNER JOIN users ON messages.id = users.id WHERE text LIKE '%house%'"])
              df.loc[index,'Results'] = cur.fetchall()
  
      except sqlite3.Error as err:
          print ("[INFO] %s" % err)
  </code></pre>
  
  <p>But this throws me an error: <em>TypeError: tuple indices must be integers or slices, not str</em>.
  I'm obviously doing something wrong and I would much appreciate any tips that would point towards an answer.</p>