python mysql执行挂起

2024-10-04 09:25:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用python来控制mysql数据库,用于一个自动浇花项目

我使用MySQLdb,一切正常,但最近我遇到了一个错误

def changeSize(self, size, table):
    command = "select * from Feuchtigkeitswerte order by ID desc limit 1"
    self._curs.execute(command)
    lastRow = self._curs.fetchone()
    if len(lastRow)-3 < size:
        command = "alter table Feuchtigkeitswerte "
        for x in range(len(lastRow)-2, size):
            command += "add column Ort_{:d} NUMERIC, ".format(x)
        command += "add column Ort_{:d} NUMERIC".format(size)
        print command
    if len(lastRow)-3 > size:
        command = "alter table Feuchtigkeitswerte "
        for x in range(size+1,len(lastRow)-3):
            command += "drop Ort_{:d}, ".format(x)
        command += "drop Ort_{:d}".format(len(lastRow)-3)
        print command
    self._curs.execute("SET autocommit=1");
    self._curs.execute(command)
    print("Checkpoint");
    self._curs.execute("SET autocommit=0");

当我执行此函数并尝试更改“金额”列时,我得到以下结果:

Start
FWerte erhalten: [2, 3, 4, 5, 6, 7]
alter table Feuchtigkeitswerte add column Ort_4 NUMERIC, add column Ort_5 NUMERIC, add column Ort_6 NUMERIC

我加了一行

print("Checkpoint")

检查一下,他是否经过

self._curs.execute(command)

但他显然没有通过。我试过自动提交之类的东西,但都没用,有人能帮我吗


Tags: selfaddformatexecutesizelentablecolumn