【问题标题】:Suppress cursor.execute() message in python MySQLdb在 python MySQLdb 中抑制 cursor.execute() 消息
【发布时间】:2012-10-26 15:56:39
【问题描述】:

如何在 MySQLdb 中抑制 cursor.execute() 消息。

>>> from warnings import filterwarnings
>>> import MySQLdb
>>> filterwarnings('ignore', category = MySQLdb.Warning)
>>> db = MySQLdb.connect('127.0.0.1', 'root', '','')
>>> cursor = db.cursor()
>>> cursor.execute("select version()")
1L

我需要禁止显示此“1L”消息

【问题讨论】:

  • 这不是消息,这是返回值
  • @martin,谢谢。我可以抑制这个值吗?
  • 把它赋值给一个变量?为什么需要压制它?
  • 我正在遍历一个文本文件并将每条记录插入 mysql 表中。对于每个 cursor.execute(),我都会得到这个值,这在我的情况下很烦人。但是分配给变量可能是个好主意。

标签: python cursor mysql-python


【解决方案1】:

你看到的不是警告信息,而是cursor.execute()的返回值。这是受影响的行数,1。

API 恰好返回一个 Python long integer,但在其他方面与常规 int 值相同:

>>> 1L
1L
>>> 1
1
>>> 1 == 1L
True

如果您不希望 Python 控制台将返回值回显给您,请将它们分配给一个变量:

>>> somevariable = 1L

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-27
    • 1970-01-01
    相关资源
    最近更新 更多