下面的代码在使用MySQLDB的python2中完美运行,我怎样才能使它与Python3兼容?
我已经调试并搜索了类似的问题。
错误: 在以下情况中忽略了例外:>回溯(最近一次呼叫最后):文件> > > " /usr/local/lib/python3.4/dist-packages/pymysql/cursors.py" ;,第41行,在del文件中> " /usr/local/lib/python3.4/dist-packages/pymysql/cursors.py",第47行,关闭ReferenceError:弱引用的对象不再存在 -
class Database():
def __init__(self):
self.host = 'localhost'
self.user = 'user'
self.password = 'pwd'
self.db = 'dbname'
self.connection = pymysql.connect(host=self.host, user=self.user, passwd=self.password, db=self.db,use_unicode=True, charset="utf8")
self.cursor = self.connection.cursor()
def storeToDB(self,ID,string,g,e):
import datetime
curr_time = datetime.datetime.now()
status = 0
try:
self.cursor.execute("""INSERT INTO master_data (`job_id`,`sstring`,`grl`,`erl`,`status`,`insert_timestamp`) \
VALUES (%s,%s,%s,%s,%s,%s)""",(jobID,search_string,g,e,status,curr_time))
self.connection.commit()
except:
self.connection.rollback()
if __name__ == "__main__":
db = Database()
db.storeToDB(20,"hello","something.com","e.com")
答案 0 :(得分:0)
尝试在db操作完成之前做一些选择:
cursor.close()
connection.close()