title: I would like some advice why this would not insert data into my SQL table
body: <p>Following is my code:</p>
  
  <pre><code>import MySQLdb
  def insert_popularity(PersonNumber, Category, Value):
  
  # make a connection to the dataabse
  connection = MySQLdb.connect(host='localhost', user='root', \
                               passwd='password', db='inb104')
  
  # get a cursor on the database
  cursor = connection.cursor()
  
  # construct the SQL statement
  sql = ("""INSERT INTO popularity (PersonNumber, Category, Value)
          VALUES(%s, %s, %s)""", (number, category, data))
  
  def open_file(filename):        
      txt_file = file(filename, 'r')
      for line in txt_file:
          # Split the line on whitespace
          for value in line.split():                
              return value
  
              number = value[0]
              data = value[1]
  
  # execute the query
  cursor.execute(sql)
  
  # commit the changes to the database\
  connection.commit()
  
  # close the cursor and connection
  cursor.close()
  
  connection.close()
  </code></pre>
  
  <p><strong>Update:</strong></p>
  
  <p>After changing my code as per <a href="https://stackoverflow.com/questions/6148293/i-would-like-some-advice-why-this-would-not-insert-data-into-my-sql-table/6148365#6148365">Paulo's suggestion</a> I now get this error:</p>
  
  <pre><code>query() argument 1 must be string or read-only buffer, not tuple. 
  </code></pre>
  
  <p>I am not sure what it is after trying to change my code:</p>
  
  <pre><code>def insert_popularity(Category, filename, cursor):
  
      txt_file = file(filename, 'r')
      for line in txt_file:
              # Split the line on whitespace
          number, value = line.split()
              # construct the SQL statement
          sql = ("""INSERT INTO popularity (PersonNumber, Category, Value)
                  VALUES(%s, %s, %s)""", (number, Category, value))
              # execute the query
          cursor.execute(sql)
  
  connection = MySQLdb.connect(host='localhost', user='root', \
                                   passwd='password', db='dogs')
  
  cursor = connection.cursor()
  
  Category = 'dogs'
  insert_popularity(Category, 'dogs.txt', cursor)
  connection.commit()
  cursor.close()
  connection.close()
  </code></pre>