I have been working on code that overrides the MySQLdb.converters.conversions dictionary. However, I have observed that this causes the created connection instance's "string_literal" function to get garbage collected.
Traceback ...
...
File "/usr/local/lib/python2.4/site-packages/MySQLdb/connections.py", line
146, in string_literal
return self.string_literal(obj)
ReferenceError: weakly-referenced object no longer exists
I'm trying to deprecate some old code and introduce the new code. However, this is happenning with some difficulty.
conn_args=dict(user='user',passwd='passwd',host='my_host',db='inclause')importtimeimportMySQLdbimportMySQLdb.converters# mysql> create database inclause;# Query OK, 1 row affected (0.02 sec)## mysql> use inclause# Database changed# mysql> create table x (a int(11), b varchar(10), c varchar(10));# Query OK, 0 rows affected (0.15 sec)## mysql> insert into x values(1, 'hello', 'world');# Query OK, 1 row affected (0.02 sec)## mysql> insert into x values(2, 'hello', 'dolly');# Query OK, 1 row affected (0.01 sec)## mysql> insert into x values(3, 'bye', 'world');# Query OK, 1 row affected (0.02 sec)## mysql> insert into x values(4, 'bye', 'dolly');# Query OK, 1 row affected (0.02 sec)## mysql> insert into x values(5, 'what up', 'world');# Query OK, 1 row affected (0.04 sec)## mysql> insert into x values(6, 'what up', 'dolly');# Query OK, 1 row affected (0.03 sec)my_conversions=MySQLdb.converters.conversions.copy()defInClause2Str(o,d):default_conv=MySQLdb.converters.escapeconverted=list()forxino:conv=d.get(x.__class__,default_conv)converted.append(conv(x,d))return'('+','.join(converted)+')'classInClause(list):passmy_conversions[InClause]=InClause2Strconversions=MySQLdb.converters.conversionssql_proposed="select*fromxwherebin%(b)sandc=%(c)s"sql_deprecated="select*fromxwherebin(%s)andc=%%(c)s"args=dict()args['b']=InClause(['hello','what up'])args['c']='dolly'conn=MySQLdb.connect(conv=my_conversions,**conn_args)try:cursor=conn.cursor()try:printtime.ctime(time.time())# This is what I would like to usecursor.execute(sql_proposed,args)print'proposed results:',cursor.fetchall()# This code will be deprecated throughout the code base.arg_str=','.join(map(conn.string_literal,['hello','what up']))sql_real=sql_deprecated%(arg_str,)cursor.execute(sql_real,args)print'deprecated results:',cursor.fetchall()printfinally:cursor.close()time.sleep(60)finally:conn.close()
According to the documentation for the MySQLdb package, what I have done is the best way to do it. However, would it be safer to add the new converter to the connection object and not pass it in as the conv keyword to the connection.
I have been working on code that overrides the MySQLdb.converters.conversions dictionary. However, I have observed that this causes the created connection instance's "string_literal" function to get garbage collected.
I'm trying to deprecate some old code and introduce the new code. However, this is happenning with some difficulty.
According to the documentation for the MySQLdb package, what I have done is the best way to do it. However, would it be safer to add the new converter to the connection object and not pass it in as the conv keyword to the connection.
Alternatively, is there a wiser way to be doing things?