Mysql.connector Python,已经使用连接时没有结果

时间:2018-11-29 09:47:42

标签: python-3.x mysql-python mysql-connector

我有一个简单的代码,可以通过http将消息发送到另一个webapp,并检查消息是否已正确插入数据库中(两次)

因此不是此代码插入数据库中(这是在另一个应用程序中完成的)

    SELECT_TABLE1_BY_ID_AND_DATE = "SELECT * FROM table1 WHERE table1.id = %s AND timedata = FROM_UNIXTIME(%s)"
    SELECT_TABLE2_BY_ID_AND_DATE = "SELECT * FROM table2 WHERE table2.id = %s AND timedata = FROM_UNIXTIME(%s)"
    try:
        conn = mysql.connector.connect(user=db['user'], password=db['password'], host=db['host'], port=db['port'], database="TEST", raise_on_warnings=True)
        cursor = conn.cursor()
        self.send1Message(msg1) # Send to HTTP Webapp
        cursor.execute(SELECT_TABLE1_BY_ID_AND_DATE, (idD, timing))
        print(cursor.fetchall()) #1
        self.send2Message(msg2) Send to HTTP Webapp
        cursor.execute(SELECT_TABLE2_BY_ID_AND_DATE, (idD, timing))
        print(cursor.fetchall()) #2 
    except mysql.connector.Error as err:
        print("Something went wrong: {}".format(err))
  • 如果我在2个sendTable之间使用相同的SQL连接,则仅第一个fetchAll返回数据。 ((#1打印数据,#2打印空列表)

  • 我还尝试在#1之后关闭连接并启动另一个连接。它适用于((#1也会打印数据,#2也会打印)

(我必须精确地确定我的查询是正确的,并且webapp可以将数据正确地及时插入数据库中)。

连接是否正常?

非常感谢!

1 个答案:

答案 0 :(得分:0)

尝试2个连接...

    conn1 = mysql.connector.connect(user=db['user'], password=db['password'], host=db['host'], port=db['port'], database="TEST", raise_on_warnings=True)
    conn2 = mysql.connector.connect(user=db['user'], password=db['password'], host=db['host'], port=db['port'], database="TEST", raise_on_warnings=True)
    cursor1 = conn1.cursor()
    self.send1Message(msg1) # Send to HTTP Webapp
    cursor1.execute(SELECT_TABLE1_BY_ID_AND_DATE, (idD, timing))
    print(cursor1.fetchall()) #1
    self.send2Message(msg2) Send to HTTP Webapp
    cursor2 = conn2.cursor()
    cursor2.execute(SELECT_TABLE2_BY_ID_AND_DATE, (idD, timing))
    print(cursor2.fetchall()) #2