Insert statement MySQL via python
Question: I am trying to make a python script that adds a video url and time to my
videopost table in DB. This is my 3rd script at python. I am still learning.
Also please tell me how to lower the execution time of this script.

Code:

    
    
      #!/usr/bin/python
      import MySQLdb
      import sys
    
      db = MySQLdb.connect (host = "host.com", user="myusername",passwd="pass", db = "dbname")
    
      cursor = db.cursor ()
      db.query("SELECT now()")
      result = db.use_result()
      time= str("%s" % \
          result.fetch_row()[0])
      print time
      db.close()
      cursor =db.cursor()
      vidurl = raw_input("Enter the URL to the video:")
      print vidurl
    
      cursor.execute ("""INSERT INTO videopost(vidurl, time) VALUES (%s, %s)""", (vidurl,time))
      db.commit()
      db.close()
    

The error i am getting in this is

    
    
      File "vidup.py", line 28, in <module>
      cursor.execute ("""INSERT INTO videopost(vidurl, time) VALUES (%s, %s)""", (vidurl,time))
      File "/usr/local/lib/python2.6/site-packages/MySQL_python-1.2.3-py2.6-freebsd-8.2-RELEASE-amd64.egg/MySQLdb/cursors.py", line 155, in execute _mysql_exceptions.InterfaceError: (0, '')
    

Well thanks for the help. For people looking for solution i am posting the
correction below:

    
    
      cursor.execute("SELECT now()")
      result = cursor.fetchall()
      time= str("%s" % \
            result[0])
      print time
      vidurl = raw_input("Enter the URL to the video:")
      print vidurl
    


Answer: You are closing the connection and then trying to grab a cursor:

    
    
    db.close()
    cursor =db.cursor()
    

Remove the first line and everything should work.

