Python和MySQLdb警告
我有一个导出MSSQL数据并将其导入MySQL的程序。我有一个正在导入的函数,如下所示:Python和MySQLdb警告,python,mysql,warnings,mysql-python,Python,Mysql,Warnings,Mysql Python,我有一个导出MSSQL数据并将其导入MySQL的程序。我有一个正在导入的函数,如下所示: def importMySql (mycursor,exportedfilename,table,delimiter): file_loc = str(sys.path[0] +"\\" +exportedfilename.lower()+".out").replace("\\", "\\\\") mycursor.execute("LOAD DATA LOCAL INFILE '%s' I
def importMySql (mycursor,exportedfilename,table,delimiter):
file_loc = str(sys.path[0] +"\\" +exportedfilename.lower()+".out").replace("\\", "\\\\")
mycursor.execute("LOAD DATA LOCAL INFILE '%s' INTO TABLE %s FIELDS TERMINATED BY '%s' LINES TERMINATED BY '\r\n'" %(str(file_loc), table, delimiter))
光标(MySQLdb)发出以下警告:
C:\Users\tfy\Documents\PyProj\UTL (Export, Import, RDF)\eic.py:98: Warning: Data truncated for column 'DateofCharges' at row 1194
mycursor.execute("LOAD DATA LOCAL INFILE '%s' INTO TABLE %s FIELDS TERMINATED BY '%s' LINES TERMINATED BY '\r\n'" %(str(file_loc), table, delimiter))
C:\Users\tfy\Documents\PyProj\UTL (Export, Import, RDF)\eic.py:98: Warning: Data truncated for column 'DateofCharges' at row 2009
mycursor.execute("LOAD DATA LOCAL INFILE '%s' INTO TABLE %s FIELDS TERMINATED BY '%s' LINES TERMINATED BY '\r\n'" %(str(file_loc), table, delimiter))
C:\Users\tfy\Documents\PyProj\UTL (Export, Import, RDF)\eic.py:98: Warning: Data truncated for column 'DateofCharges' at row 4793
mycursor.execute("LOAD DATA LOCAL INFILE '%s' INTO TABLE %s FIELDS TERMINATED BY '%s' LINES TERMINATED BY '\r\n'" %(str(file_loc), table, delimiter))
但我需要将警告控制为仅输出:
Warning: Data truncated for column 'DateofCharges' at row 1194
Warning: Data truncated for column 'DateofCharges' at row 2009
Warning: Data truncated for column 'DateofCharges' at row 4739
我环顾四周,发现了大量说明如何创建自定义警告的信息。然而,我不确定我将如何实现上述目标。我不想关闭警告,我只想“格式化”它们。我曾考虑过编辑实际的MySQLdb文件,但它是.egg格式的,无法做到这一点。我还玩了warning.format()
,但没有成功
谢谢 是否可以在子流程中运行mysql代码?如果是这样,您可以使用Python运行mysql代码,从stdout读取输出并相应地格式化它。例如,使用
process.stdout.readline()
您可以参考以下问题:是否可以在子流程中运行mysql代码?如果是这样,您可以使用Python运行mysql代码,从stdout读取输出并相应地格式化它。例如,使用
process.stdout.readline()
您可以参考以下问题:使用MySQLdb
您可以使用monkey补丁程序MySQLdb来实现这一点:
import types
def warning_check(self):
if not self._warnings:
self.messages = ()
return
self.messages = self._get_db().show_warnings()
然后在函数中按如下方式修补光标对象:
cur._warning_check = types.MethodType(warning_check, cur)
cnx.get_warnings = True
cur.execute("LOAD DATA..")
for msg in cur.fetchwarnings():
print "Warning: {msg}".format(msg=msg[2])
然后,在执行完加载数据..
后,可以打印消息:
cur.execute("LOAD DATA..")
for msg in cur.messages:
print "Warning: {msg}".format(msg=msg[2])
使用MySQL连接器/Python
使用MySQL Connector/Python,您可以执行以下操作:
cur._warning_check = types.MethodType(warning_check, cur)
cnx.get_warnings = True
cur.execute("LOAD DATA..")
for msg in cur.fetchwarnings():
print "Warning: {msg}".format(msg=msg[2])
(请注意,您需要使用MySQLdb使用连接参数设置客户端标志client\u flags=[mysql.connector.ClientFlag.LOCAL\u FILES]
)
您可以使用monkey补丁程序MySQLdb来实现这一点:
import types
def warning_check(self):
if not self._warnings:
self.messages = ()
return
self.messages = self._get_db().show_warnings()
然后在函数中按如下方式修补光标对象:
cur._warning_check = types.MethodType(warning_check, cur)
cnx.get_warnings = True
cur.execute("LOAD DATA..")
for msg in cur.fetchwarnings():
print "Warning: {msg}".format(msg=msg[2])
然后,在执行完加载数据..
后,可以打印消息:
cur.execute("LOAD DATA..")
for msg in cur.messages:
print "Warning: {msg}".format(msg=msg[2])
使用MySQL连接器/Python
使用MySQL Connector/Python,您可以执行以下操作:
cur._warning_check = types.MethodType(warning_check, cur)
cnx.get_warnings = True
cur.execute("LOAD DATA..")
for msg in cur.fetchwarnings():
print "Warning: {msg}".format(msg=msg[2])
(请注意,您需要使用连接参数设置客户端标志
client\u flags=[mysql.connector.ClientFlag.LOCAL\u FILES]
),因此这是我找到的最简单的方法。。。不知道为什么我原来没有想到这个。。。但我只是抑制了光标发出的警告:
import warnings
warnings.filterwarnings("ignore", category = MySQLdb.Warning)
然后,我将以下代码添加到importMySql函数中:
mycursor.execute("SHOW WARNINGS")
warnings = mycursor.fetchall()
for i in range(len(warnings)):
print "Warning - " +warnings[i][2]
所以这是我找到的最简单的方法。。。不知道为什么我原来没有想到这个。。。但我只是抑制了光标发出的警告:
import warnings
warnings.filterwarnings("ignore", category = MySQLdb.Warning)
然后,我将以下代码添加到importMySql函数中:
mycursor.execute("SHOW WARNINGS")
warnings = mycursor.fetchall()
for i in range(len(warnings)):
print "Warning - " +warnings[i][2]
使用pprint解决这个问题。作为OPs解决方案,需要抑制默认警告,然后添加显示警告功能,然后使用新的打印格式
from warnings import filterwarnings
import MySQLdb as mdb
from pprint import pprint
filterwarnings('ignore', category = mdb.Warning)
con = mdb.connect(...)
cur = con.cursor()
query = "Update table ..."
cur.execute(query)
con.commit()
warnings = con.show_warnings() # return in tuple type
pprint(warnings, width=100, depth=2) # width is the num of characters in each line, and depth is the level of the warnings in the tuple
使用pprint解决这个问题。作为OPs解决方案,需要抑制默认警告,然后添加显示警告功能,然后使用新的打印格式
from warnings import filterwarnings
import MySQLdb as mdb
from pprint import pprint
filterwarnings('ignore', category = mdb.Warning)
con = mdb.connect(...)
cur = con.cursor()
query = "Update table ..."
cur.execute(query)
con.commit()
warnings = con.show_warnings() # return in tuple type
pprint(warnings, width=100, depth=2) # width is the num of characters in each line, and depth is the level of the warnings in the tuple