title: Saving MySQL query results to a CSV file
body: <p>I have two queries that I'd like to save together in a single CSV file.  I can do this individually but I can't figure out how to copy the two files into a single file (not merge, just copy columns from one file to the other) so perhaps I can do this at the source with the SQL queries.</p>
  
  <pre><code>import MySQLdb, csv
  dbServer = 'localhost'
  dbUser = 'root'
  dbPass = 'password'
  dbSchema = 'Name_of_Database'
  db = MySQLdb.connect(host=dbServer, user=dbUser,passwd=dbPass,db=dbSchema)
  cursor = db.cursor()
  cursor.execute("""SELECT * FROM temperature_tbl WHERE Mac_Address = %s AND Date =   CURDATE() ORDER BY Time asc;""",(mac))
  first_set = cursor.fetchall()
  
  cursor.execute("""SELECT row1, row2, row3, row4 FROM temperature_tbl WHERE Mac_Address = "xx xx xx xx xx xx" AND Date = CURDATE() ORDER BY Time asc;""")
  second_set = cursor.fetchall()
  </code></pre>
  
  <p>Now that I have the tuple results from these two queries is it possible to write them all at the same time within a CSV File?  For instance:</p>
  
  <pre><code>csv_file = open('filename', 'w')
  writer = csv.writer(csv_file)
  writer.writerow(['col1', 'col2', 'col3', 'col4', 'col5', etc etc etc])
  writer.writerows(first_set + second_set)
  csv_file.close()
  </code></pre>
  
  <p>As long as the results of the two queries matches the row header this would be perfect.</p>
  
  <p>EDIT1:  Ok how about this.  How would I join two tuple of tuples together so that I am not extending the length of the tuples, but joining them in parallel?  If I just add tuple1 + tuple2 it extends the length of the list but I want them side by side.  EG:</p>
  
  <pre><code>Tuple1:    Tuple2:
  x,x,x,x    y,y,y,y
  x,x,x,x    y,y,y,y
  x,x,x,x    y,y,y,y
  </code></pre>
  
  <p>If I do tuple1 + tuple2 I get:</p>
  
  <pre><code>x,x,x,x
  x,x,x,x
  x,x,x,x
  y,y,y,y
  y,y,y,y
  y,y,y,y
  </code></pre>
  
  <p>What I really want is:</p>
  
  <pre><code>x,x,x,x,y,y,y,y
  x,x,x,x,y,y,y,y
  x,x,x,x,y,y,y,y
  </code></pre>
  
  <p>Then I can writerows() and modify the CSV by removing extra columns (for instance Date is referenced twice).</p>
  
  <p>Is this possible?</p>
  
  <p>EDIT2 - SOLUTION</p>
  
  <p>Here is what I did.  I made several SQL queries and then list.appended each query result into a large list.  This extended horizontally like I wanted it to.  Then  I was able to writerows() to get a complete CSV file the way I wanted it.</p>