Python Forum
mysql connector/telnet issue (re: text game) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Networking (https://python-forum.io/forum-12.html)
+--- Thread: mysql connector/telnet issue (re: text game) (/thread-8070.html)



mysql connector/telnet issue (re: text game) - rebubula76 - Feb-05-2018

"Hi y'all, had a question about mysql connector and a text game I'm creating. I'm trying to make it so that a user will type something in (defined as "command" in my script), and then it will connect to my database and check their input command against a table, and return back a message associated with that command. For example, the player types a command that isn't part of the game yet, like "cry", and a table I've set up in my database has the first column as "idemotes", the second column as "emoteMsg". I want the game to output the "emoteMsg" to the player when they type in "cry", which might be "You sob like a little child." Snippet of my code is below:"
------------------------------------
else:
                 
            def dbLookup(command):
                #connect to the database
                cnx = mysql.connector.connect(user='xxxx',
                                          password='xxxx',
                                          host='xxxx',
                                          port='xxxx',
                                          database='commands')
                #point the cursor to the database
                cursor = cnx.cursor()
                
                query = ("SELECT emoteMsg FROM emotes WHERE idemotes = '%s'")

                #perform the query and link %s to the player's command:
                cursor.execute(query, command)

                for (myMsg) in cursor:
                    mud.send_msg(id, "{}".format(emoteMsg))
                
                cursor.close()

                cnx.close()

            
            dbLookup(command)
-----------------------------
"It works fine if I substitute '%s' in the query for an actual idemote value in the table, like 'cry'. But gets hung up if I put in that '%s', that I thought would match the command the player puts in to the idemote in the table and return only the emoteMsg value in that case. Feel like I'm getting some syntax incorrect here. Thanks in advance for any help!"


RE: mysql connector/telnet issue (re: text game) - rebubula76 - Feb-06-2018

Nevermind, figured out the answer:

Need to make the query into a tuple, so mySQL can understand you. Stupid commas....

query = ("SELECT emoteMsg FROM emotes WHERE idemotes = '%s'", (command,))

:)