Python mysql-connector提交不起作用

时间:2017-03-23 22:11:15

标签: python mysql commit python-multithreading

所以我已经好几个小时了,而且还有一些非常奇怪的事情。

我循环遍历代码以检查表值是否已更改。如果有的话,运行一些代码。

当我运行以下程序时,一切正常! “锁定状态”应该从打开到关闭。

    import time
    import mysql.connector

    host = '...'
    database = '...'
    user = '...'
    password = '...'

    conn = mysql.connector.connect(host=host, database=database, user=user, password=password)
    conn.start_transaction(isolation_level='READ COMMITTED')

    state = 'Open'

    def get_web_lock_request():
        try:
            cursor = conn.cursor()
            cursor.execute("SELECT * FROM `doorlock`.`state`")
            state = ''.join(cursor.fetchone())
            cursor.close()
            return state
        except mysql.connector.Error as e:
            print 'get_lock_state ' + format(e)
            return ''

    def set_web_lock_request():
        try:
            cursor = conn.cursor()
            cursor.execute("UPDATE `doorlock`.`state` SET `state`='" + state + "'")
            conn.commit()
            cursor.close()
        except mysql.connector.Error as e:
            print 'set_lock_state: ' + format(e)

    while 1:
        dbstate = get_web_lock_request()
        set_web_lock_request()

        if state == 'Open':
            state = 'Closed'
        else:
            state = 'Open'

        time.sleep(2)
        print dbstate

但在我的主要代码中,“倾听变化”是一个主题。由于某种原因,UPDATE语句不起作用!即使使用conn.commit()

import mysql.connector

conn = mysql.connector.connect(host=host, database=database, user=user, password=password)
conn.start_transaction(isolation_level='READ COMMITTED')

def get_web_lock_request():
    try:
        cursor = conn.cursor()
        cursor.execute("SELECT * FROM `doorlock`.`state`")
        state = ''.join(cursor.fetchone())
        cursor.close()
        return state
    except mysql.connector.Error as e:
        print 'get_lock_state ' + format(e)
        return ''

def set_web_lock_request(state):
    try:
        cursor = conn.cursor()
        cursor.execute("UPDATE `doorlock`.`state` SET `state`='" + state + "'")
        conn.commit()
        cursor.close()
    except mysql.connector.Error as e:
        print 'set_lock_state: ' + format(e)

def web_request_listener():
    global updating
    try:
        while 1:

            print 'get_web_lock_request : ' + get_web_lock_request()
            print 'current_state : ' + current_state()

            if get_web_lock_request() != current_state() and not updating:
                if get_web_lock_request() == 'Open':
                    open_lock()
                    print 'DoorLock: State is now open'
                elif get_web_lock_request() == 'Closed':
                    close_lock()
                    print 'DoorLock: State is now closed'

            time.sleep(1)

    except KeyboardInterrupt:
        exit()

if __name__=='__main__':
    try:
        button_listener_thread = threading.Thread(target=button_listener)
        web_request_listener_thread = threading.Thread(target=web_request_listener)

    except KeyboardInterrupt:
        exit()

我真的可以使用一些帮助,我几乎没有别的尝试:)

弗洛

0 个答案:

没有答案