【问题标题】:Error when controlling mysql with python class用python类控制mysql时出错
【发布时间】:2018-09-28 06:08:33
【问题描述】:

我正在尝试使用类系统更新 mysql 数据库,但无法使更新部分正常工作。以前的方法没问题,但我想使用具有异常错误控制的类系统。有人可以让我知道我做错了什么。目前,对于这个脚本,我只是想将变量 Boileron 发送到数据库列办公室。

import MySQLdb

class DBSolar:
    conn = None

    def connect(self):
        try:
            self.conn = MySQLdb.connect("192.xxx.x.x", "exxxxx", "Oxxxx", "hxxxx")
        except (MySQLdb.Error, MySQLdb.Warning) as e:
            print (e)
            self.conn = None
        return self.conn

    def query(self, sql):
        try:
            cursor = self.conn.cursor()
            cursor.execute(sql)
        except (AttributeError, MySQLdb.OperationalError):
            self.connect()
            cursor = self.conn.cursor()
            cursor.execute(sql)
        return cursor

    def update(self, task):
        boilerState = task
        try:
            sql = "UPDATE dashboard SET office = ? WHERE id = 1", (boilerState)
            cursor = self.conn.cursor()
            cursor.execute(sql)
        except (AttributeError, MySQLdb.OperationalError):
            self.connect()
            cursor = self.conn.cursor()
            cursor.execute(sql)
        return

while 1:

    BoilerOn = 1
    print BoilerOn

    dbSolar = DBSolar()
    connSolar = dbSolar.connect()

    if connSolar:
        dbSolar.update(BoilerOn)

下面是 putty 控制台的错误报告

 Traceback (most recent call last):
   File "test2.py", line 47, in <module>
     dbSolar.update(BoilerOn)
   File "test2.py", line 29, in update
     cursor.execute(sql)
   File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 223, in execute
     self.errorhandler(self, TypeError, m)
   File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorvalue
 TypeError: query() argument 1 must be string or read-only buffer, not tuple

【问题讨论】:

    标签: python mysql


    【解决方案1】:

    得到这个工作将更新更改为以下

    def update(self, task):
        try:
            cursor = self.conn.cursor()
            cursor.execute("UPDATE dashboard SET office = %s WHERE id = 1", [task])
            self.conn.commit()
        except (MySQLdb.Error, MySQLdb.Warning) as e:
            print (e)
            self.connect()
            #cursor = self.conn.cursor()
            #cursor.execute(sql)
            #self.connect.commit()
        return
    

    【讨论】:

      【解决方案2】:

      您的sql 创建了一个元组,但cursor.execute(statement, params) 期待statement字符串元组字典 em> 为params

      对于 MySQL UPDATE,您还需要提交执行。

      因此尝试:

      self.conn.cursor.execute("UPDATE dashboard SET office = %s WHERE id = 1", (str(task), ))
      self.conn.commit()
      

      我建议您阅读'MySQL Connector/Python Development Guide 更好地了解在 python 中使用 MySQL。

      【讨论】:

      • 这已经停止了元组错误,但它没有更新数据库中的办公室行。我将不得不对此进行更多研究
      • 在你的 MySQL 上直接运行 SQL 看看你是否有同样的问题
      【解决方案3】:

      MySQL Developer document

      由于默认情况下 Connector/Python 关闭自动提交,并且 MySQL 5.5 及更高版本默认使用事务 InnoDB 表,因此有必要使用连接的 commit() 方法提交更改。您也可以使用 rollback() 方法进行回滚。

      您需要在cursor.execute(sql) 之后添加self.conn.commit() 以提交所有更改。

      或者开启autocommit,在Python mySQL Update, Working but not updating table中描述

      【讨论】:

      • 我假设你的意思是我尝试过的 cursor.execute(sql) self.conn.commit() ,但它仍然出现错误。我将使用错误更新我的原始帖子
      猜你喜欢
      • 2018-04-15
      • 2017-10-30
      • 1970-01-01
      • 1970-01-01
      • 2017-05-08
      • 1970-01-01
      • 2022-12-15
      • 2013-06-19
      • 1970-01-01
      相关资源
      最近更新 更多