我有一个导出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' 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()
,但没有成功。
谢谢!
答案 0 :(得分:1)
你可以修补MySQLdb来实现这个目标:
import types
def warning_check(self):
if not self._warnings:
self.messages = ()
return
self.messages = self._get_db().show_warnings()
然后在函数中修补Cursor对象,如下所示:
cur._warning_check = types.MethodType(warning_check, cur)
然后,当您完成执行LOAD DATA..
时,您可以打印消息:
cur.execute("LOAD DATA..")
for msg in cur.messages:
print "Warning: {msg}".format(msg=msg[2])
使用MySQL Connector / Python,您可以这样做:
cnx.get_warnings = True
cur.execute("LOAD DATA..")
for msg in cur.fetchwarnings():
print "Warning: {msg}".format(msg=msg[2])
(请注意,您需要使用连接参数client_flags=[mysql.connector.ClientFlag.LOCAL_FILES]
设置的客户端标志)
答案 1 :(得分:1)
所以这是我找到的最简单的方法......不知道为什么我最初没想到这个......但我只是简单地压制了光标发出的警告:
import 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]
答案 2 :(得分:1)
想出来使用pprint。作为OP解决方案,需要禁止默认警告,然后添加show_warnings函数,然后使用新的打印格式。
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
答案 3 :(得分:0)
可以在子进程中运行mysql代码吗?如果是这样,您可以使用Python的subprocess来运行mysql代码,从stdout读取输出并相应地对其进行格式化。例如,使用process.stdout.readline()
。
您可以参考以下问题:Starting and Controlling an External Process via STDIN/STDOUT with Python