mysql - Python,函数拒绝更新 MySQL 值?

标签 mysql python-3.x raspberry-pi3

我正在为我的 Raspberry Pi3 编写灌溉脚本,但在更新数据库时遇到了一些问题。

这是我当前的代码(在循环中):

nirrigationtime = getseconds('temp_average','humidaverage','press_Average')
print("has received data from function about how much time to add, which is currently "),nirrigationtime

#Get the current database irrigation seconds value
current_seconds = dbfetch('NIGHT_SECONDS','weather_settings')
print("this is the current value in the database for irrigation "),current_seconds

#Update the current seconds value with the additonal seconds from the getseconds function
nirriupdated = nirrigationtime + current_seconds
print("new irrigation value will be "),nirriupdated

#Update database with new night time irrigation value
try:
    dbupdate('NIGHT_SECONDS','weather_settings','nirriupdated')
    print("database updated, sleeping for 1.5min")
except:
    print("update actaully failed?")

#Sleep for 1.5 minutes
time.sleep(90)

这会在终端中产生以下结果:

has received data from function about how much time to add, which is currently  10
this is the current value in the database for irrigation  50.0
new irrigation value will be  60.0
database updated, sleeping for 1.5min

然而,问题是它永远不会将更新后的值写入数据库,而且我正在为它抓狂,因为更新功能在项目的所有其他部分都运行良好。

这是数据库更新函数:

#This function connects to database and updates the value in the selected column in the selected table to the set new value
def dbupdate(dbcolumn,dbtable,newvalue):
    try:
        db = MySQLdb.connect("localhost","user","password","weather")
        cursor = db.cursor()
        sql = "UPDATE "+dbtable+" SET "+dbcolumn+" = "+newvalue
        try:
            cursor.execute(sql)
            db.commit()

        except:
            db.rollback()
        cursor.close()
        db.close()
        return
    except:
        print("Database connection failed")

数据库列设置为 Decimal(5,2),如上所述,该函数在其他地方运行良好。

有人能看到我看不到的东西吗?

最佳答案

看来我暂时解决了这个问题。 通过将 dbupdate() 函数从我的外部functions.py文件(我总是这样做并将它们导入到我正在处理的文件中)移动到其余代码所在的当前文件,并添加 nirriupdated = str(nirriupdated )。我意识到我试图将一个整数(nirriupdated)附加到一个字符串(sql 变量),但这不起作用。

工作代码如下所示:

nirrigationtime = getseconds('temp_average','humidaverage','press_Average')

#Get the current database irrigation seconds value
current_seconds = dbfetch('NIGHT_SECONDS','weather_settings')

#Update the current seconds value with the additonal seconds from the getseconds function
nirriupdated = nirrigationtime + current_seconds
nirriupdated = str(nirriupdated)

#Update database with new night time irrigation value - For some reason, this doesn't work?
db = MySQLdb.connect("localhost","user","password","weather")
cursor = db.cursor()
sql = "UPDATE weather_settings SET NIGHT_SECONDS = "+nirriupdated
try:
    cursor.execute(sql)
    db.commit()

except:
    db.rollback()
cursor.close()
db.close()


#Sleep for 15 minutes
time.sleep(900)

如果有人可以帮助我理解为什么在外部functions.py 中运行时这不起作用,而不是在函数写入文件中(其余代码位于其中)时,那么我将非常感激。特别是因为我在外部文件中使用了相同的函数,并且在该文件中更靠前的位置,没有出现任何问题。就是这样的事情让我彻夜难眠......

关于mysql - Python,函数拒绝更新 MySQL 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50887451/

相关文章:

mysql - 从包含 ID、姓名、薪水三列的表中查找第二高薪水

MySQL 喜欢不喜欢 `\` ?

mysql - Spring JPA 查询涉及 COUNT() 和 LOWER()

Python\csv.writer 删除以前的数据而不是向文件添加新数据

python - 寻找 html.unescape ("&nbsp")

c - 使用C读取RaspberryPi上的声卡数据

python - 什么是无效解析元组?

mysql - 索引和连接

Python在换行符处将随机整数写入文件

Python和覆盆子picamera对象计数,多次计数