Python / MySQL:在for循环中选择数据

时间:2015-04-10 00:37:57

标签: python mysql database tkinter mysql-python

如果看起来我在解决问题时遇到一些麻烦,我道歉。基本上,我正在编写一个消息系统,用户可以在其中向其他用户发送消息并在收件箱中查看他们的消息。以下是我的数据库和GUI的截图: http://imgur.com/a/ra5Di

这是我在收件箱中显示消息的功能:

#DISPLAY MESSAGE IN INBOX FUNCTION
def displayMessageInbox(mf, c, vm):
    recipient = config.nameInput

    cursor.execute("""SELECT CONCAT('Title: ', m.title, '\n',
                    'From: ', s.sender, '\n',
                    'Sent: ', s.timeSent)
                    FROM Message m
                    INNER JOIN SentMessage s ON m.id = s.messageID
                    WHERE s.recipient = %s""", (recipient))
    rows = cursor.fetchall()

    i = 3
    for message in rows:
        messageInfo = tk.Label(mf, text=message, bg="white", anchor=tk.W,
                               justify=tk.LEFT, width=30)
        messageInfo.grid(row=i, column=0, sticky=tk.W)
        viewReply = tk.Button(mf, text="View/Reply", anchor=tk.W,
                      command=lambda:c.show_frame(vm))
        viewReply.grid(row=i, column=1,padx=10, sticky=tk.W)
        i+=1

这是我的功能,用于在用户点击"查看/回复后显示消息及其内容。"请注意,它正在选择特定的消息ID,而不是我实际想要显示的消息ID。换句话说,它只是占位符。

#READ MESSAGE CONTENT FUNCTION
def readMessage(mf):
    mID = 'a6bb9c97-0f59-44ea-9f2b-759ffbac5031'

    cursor.execute("""SELECT CONCAT('From: ', sm.sender, '\n',
                'Title: ', m.title, '\n',
                'Message: ', m.content)
                FROM Message m
                INNER JOIN SentMessage sm ON m.id = sm.messageID
                WHERE m.id = %s""", (mID))
    rows = cursor.fetchone()

    message = rows[0]
    readMessageLabel = tk.Label(mf, text=message, bg="white", anchor=tk.W,
                                justify=tk.LEFT, width=100)
    readMessageLabel.grid(row=3, column=0, padx=10, sticky=tk.W)

所以我的问题是这个......我希望用户能够点击"查看/回复"并查看相应的消息,而不仅仅是占位符。我甚至不知道如何开始解决这个问题。我认为我想要做的是从displayMessageInfo中的特定行获取消息id for循环,但我并不完全确定。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

显然,mID是一个静态值。在用户按下“查看/回复”后,您需要通过将其值传递到readMessage函数以条件相应的消息来使该变量动态化。

虽然我不知道应用程序界面控件的设置,但考虑:1)查询消息ID,2)将其保存到变量,3)将其传递给函数。也许这可以让你开始:

def displayMessageInbox(mf, c, vm):
    recipient = config.nameInput

    cursor.execute("""SELECT m.id, CONCAT('Title: ', m.title, '\n',
                    'From: ', s.sender, '\n',
                    'Sent: ', s.timeSent) As messagetxt
                    FROM Message m
                    INNER JOIN SentMessage s ON m.id = s.messageID
                    WHERE s.recipient = %s""", (recipient))

    i = 3
    for message in cursor.fetchall():
          mID = message[0]
          messagetxt = message[1]
          ...

          if (viewReply == somevalue): # ENTER LOGIC TO CAPTURE WHICH MESSAGE USER SELECTED     
                 readMessage(mf, mID)

def readMessage(mf, mID):

    cursor.execute("""SELECT CONCAT('From: ', sm.sender, '\n',
            'Title: ', m.title, '\n',
            'Message: ', m.content)
            FROM Message m
            INNER JOIN SentMessage sm ON m.id = sm.messageID
            WHERE m.id = %s""", (mID))
    rows = cursor.fetchone()

    message = rows[0]
    ...