Mysqldb interfaceError

I have a very strange problem with mysqldb (mysql module for python).

I have a query file for inserting records into tables. If I call functions from a file, it works fine; but when you try to call one of the functions from another file, it throws me

_mysql_exception.InterfaceError: (0, '')

I really don't understand what I'm doing wrong here.

I call the function from buildDB.py :

 import create create.newFormat("HD", 0,0,0) 

The newFormat (..) function is in the create.py file (imported):

 from Database import Database db = Database() def newFormat(name, width=0, height=0, fps=0): format_query = "INSERT INTO Format (form_name, form_width, form_height, form_fps) VALUES ('"+name+"',"+str(width)+","+str(height)+","+str(fps)+");" db.execute(format_query) 

And the class database is as follows:

import MySQLdb from MySQLdb.constants import FIELD_TYPE

 class Database(): def __init__(self): server = "localhost" login = "seq" password = "seqmanager" database = "Sequence" my_conv = { FIELD_TYPE.LONG: int } self.conn = MySQLdb.connection(host=server, user=login, passwd=password, db=database, conv=my_conv) # self.cursor = self.conn.cursor() def close(self): self.conn.close() def execute(self, query): self.conn.query(query) 

(I will put only the corresponding code)

Tracking:

 Z:\sequenceManager\mysql>python buildDB.py D:\ProgramFiles\Python26\lib\site-packages\MySQLdb\__init__.py:34: DeprecationWa rning: the sets module is deprecated from sets import ImmutableSet INSERT INTO Format (form_name, form_width, form_height, form_fps) VALUES ('HD',0 ,0,0); Traceback (most recent call last): File "buildDB.py", line 182, in <module> create.newFormat("HD") File "Z:\sequenceManager\mysql\create.py", line 52, in newFormat db.execute(format_query) File "Z:\sequenceManager\mysql\Database.py", line 19, in execute self.conn.query(query) _mysql_exceptions.InterfaceError: (0, '') 

Warning has never been a problem, so I don't think this is related.

+6
source share
3 answers

I got this error when trying to use a closed connection.

+19
source

The problem is resolved. I initialized the database twice. Sorry if you lost your time reading this!

+2
source

I could not get your installation to work. I give me the same error all the time. However, the way you connect and execute queries to db with the query looks "non-standard . " I got lucky with this setting:

 conn = MySQLdb.Connection(user="user", passwd="******", db="somedb", host="localhost") cur = conn.cursor() cur.execute("insert into Format values (%s,%s,%s,%s);", ("hd",0,0,0)) 

Thus, you can use the output to enter db modules, which is necessary to mitigate SQL injections.

0
source

Source: https://habr.com/ru/post/886153/


All Articles