Menu

Just print MySQL warnings, no traceback?

2002-02-26
2012-09-19
  • Murthy Kambhampaty

    I am importing a file that has missing values (nulls). I expect MySQL to issue warnings. I would like to see the warning line, but I don't want to see the traceback, i.e., I just want to print the contents of "_mysql_exceptions.Warning" below. I am, unfortunately, a Python novice. Is there a way to hack MySQLdb to make it stop printing the traceback on warnings? Can this be programmed as an option/function in the MySQLdb distribution. One might, for ex, then do:

    try MySQLdb.Warning w:
        LOAD DATA INFILE ...
        print "Warning: " % w
    except MySQLdb.error e:
        print "Error: " % e
        sys.exit (1)

    Thanks for your help,
        Murthy

    MySQLdb printout on MySQLdb warning:

    Traceback (most recent call last):
      File "D:\temp\Import_MySQL.py", line 173, in ?
        SQLImport(dbname, flist, rt, dtype)
      File "D:\temp\Import_MySQL.py", line 161, in SQLImport
        INFILE '""" +datasource +r'\\' +csvname +"""' REPLACE
      File "C:\Python\lib\site-packages\MySQLdb\cursors.py", line 61, in execute
        r = self._query(query)
      File "C:\Python\lib\site-packages\MySQLdb\cursors.py", line 168, in _query
        rowcount = self._BaseCursor__do_query(q)
      File "C:\Python\lib\site-packages\MySQLdb\cursors.py", line 118, in __do_query
        self._check_for_warnings()
      File "C:\Python\lib\site-packages\MySQLdb\cursors.py", line 150, in _check_for_warnings
        raise Warning, self._info
    _mysql_exceptions.Warning: Records: 236040  Deleted: 0  Skipped: 0  Warnings: 3616363

     
    • Andy Dustman

      Andy Dustman - 2002-03-01

      When you call connect(), add an additional keyword parameter:

          cursorclass=MySQLdb.cursors.CursorNW

      Then you won't get Warnings raised as exceptions. Else create your cursor like this:

          c = db.cursor(MySQLdb.cursors.CursorNW)

      Use c.info() after c.execute() to get what you want.

       
    • Murthy Kambhampaty

      Thanks for the help.

      I found that the first method only works, for me at least (Win2K, Python-2.2, MySQLdb 0.9.1), if I import MySQLdb with:

      from MySQLdb import *.

      The second method works even using the simpler form:

      import MySQLdb

       

Log in to post a comment.