0
  1. I am getting error message when I have con.close()- mysql.connector.errors.OperationalError: [Errno 9] Bad file descriptor
  2. I am getting error message when I remove con.close()- mysql.connector.errors.InterfaceError: 2055: Lost connection to MySQL server at 'localhost:3306', system error: 10054

Any suggestion? Thx
Below is my query:

import mysql.connector
class databaseConnection:
    def __init__ (self,settings ): 
        cur=settings.cursor()

        selectstmt=("SELECT qSQL FROM TBLTEST WHERE id = 4")
        cur.execute(selectstmt)
        res=cur.fetchone()
        qSQL=res[0]
        cur.execute(qSQL)
        qSQLresults=cur.fetchall()
        for row in qSQLresults:
            id= row[0]
            city= row[1]
            state=row[2]
            insertstmt=("""INSERT into FACTRESTTBL (id, city, state) 
                           values (%d, '%s', '%s')""" % (id, city, state))
            cur.execute(insertstmt)
            con.commit() 
        con.close() # 

print 'done'

con=mysql.connector.connect(host="localhost", 
                            user="root", 
                            password="test", 
                            database="test")
databaseobject = databaseConnection(con)

--

9
  • Shouldn't it be settings.close()? Commented Feb 20, 2013 at 20:55
  • Aside: you should pass the parameters as a second argument to .execute() to guard against SQL-injection attack. Commented Feb 20, 2013 at 20:57
  • I did similar to: stackoverflow.com/questions/14863692/… So I used con.close() How would you " pass the parameters as a second argument to .execute() to guard against SQL-injection attack" Commented Feb 20, 2013 at 21:09
  • Like this: cur.execute(insertstmt, (id, city, state)). And you would change all your placeholders to the appropriate placeholder for the module you are using. Some use ?, others use %s. Commented Feb 20, 2013 at 21:15
  • In either case, it seems that your connection is being terminated prematurely. There may be a configuration you can set for a higher timeout period. Commented Feb 20, 2013 at 21:31

1 Answer 1

0

When I run the script on Vim on server, it works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.