- I am getting error message when I have con.close()- mysql.connector.errors.OperationalError: [Errno 9] Bad file descriptor
- 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)
--
settings.close()?.execute()to guard against SQL-injection attack.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.