title: Python -- Inserting Record from For Loop using MySQLDB
body: <p>I have done the tutorials but I am far from an accomplished python hacker.  </p>
  
  <p>I am trying to do the following using MySQLdb:</p>
  
  <ol>
  <li>loop through a list of files from a directory</li>
  <li>generate a new file name for these files based on function parameters</li>
  <li>insert a database record with the new filename</li>
  <li>move the file to the new directory using the new filename.</li>
  </ol>
  
  <p>I have items 1,2 and 4 working but can't get the database insert working correctly.  There is not an error but nothing is inserted into the table.  I can login and access the database through a command prompt, and I can insert a record into the table directly.</p>
  
  <p>I am using python 2.75, mySQL 5.6.13, windows 7 64bit, and  MySQL_python-1.2.4-py2.7-win32 installed using easy_install.  </p>
  
  <pre><code>def moveFile(rPath, dPath, dbID):
  import fnmatch
  import os
  import MySQLdb as mysql
  import sys
  conn = mysql.connect(host='localhost', user='*******', passwd='*******', db='*******')
  pattern = '*.pdf'
  inc = 0
  for root, dirs, files in os.walk(rootPath):
      for filename in fnmatch.filter(files, pattern):
          dire = root.find(rootPath) + len(rootPath)
          dest = destPath + root[dire:]
          fname = dbID + "_" + str(inc) + ".pdf"
          # print fname
          # print os.path.join(root, filename),  os.path.join(dest, fname)
          # os.renames(os.path.join(root, filename), os.path.join(dest, fname))
          x = conn.cursor()
          x.execute("INSERT INTO documents(documentname) VALUES (fname)")
          inc += 1
  
  return 'Files Count: ', inc
  </code></pre>
  
  <p>I am sure I need to commit somewhere in the code but my attempts have produced no error but also no results.  </p>
  
  <p>Thanks for reading my question land I will try all suggestions promptly.  </p>