0

I'm importing files to a MySQL DB using the LOAD DATA INFILE command.
Some files may have an error which results in a console message like:

mysql.connector.errors.DatabaseError: 1265 (01000): Data truncated for column 'z' at row x

How can I put this error message to a QMessageBox so the user of the .exe has an indicator where to check the dataset?

try:
   cursor.execute(query)
except:
   QMessageBox.warning(self, "Failure", ...Console Output...)  
kero
  • 10,647
  • 5
  • 41
  • 51
blade_runner
  • 145
  • 1
  • 2
  • 12

1 Answers1

1

If the SQL library is using standard Python output, you could try to overwrite sys.stderr and sys.stdout with any object that implements a write method:

import sys

class TextBoxStderr:

    def __init__(self):
        self.textbox = QTextEdit()

    def write(self, errmsg):
        self.textbox.append(errmsg)  

box_stderr = TextBoxStderr()
sys.stderr = box_stderr

# ... Call Import Operation ...

# If any error was appended to the text box, show it
if box_stderr.textbox.toPlainText():
     box_stderr.textbox.show()

Any text sent to stderr will be appended to a QTextEdit. Make sure you rollback the original object after the operation is complete:

sys.sterr = sys.__stderr__
igortg
  • 150
  • 9
  • This works so far but at the moment I get 22 Message Boxes with chunks of the whole error message. How can I join them before displaying the message box? – blade_runner Jun 03 '14 at 15:15
  • Sorry for being picky but is there a way to just get the row out of the error message? The whole message may be to much for the user, so a QMessageBox like `QMessageBox.warning(self, "Data error", "There seems to be an error in raw date around value %s." , row)` may be more suited. – blade_runner Jun 04 '14 at 07:23
  • In the `TextBoxStderr`, define the textbox as a list() instead of a QTextEdit. Then replace the last if by: `if box_stderr.textbox: QMessageBox.warning(...)` – igortg Jun 09 '14 at 18:49