Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 mysql connector/telnet issue (re: text game)
"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:"
            def dbLookup(command):
                #connect to the database
                cnx = mysql.connector.connect(user='xxxx',
                #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))


"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!"
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,))


Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Issue when running telnet program using python3 searching1 11 2,768 Dec-22-2018, 12:48 AM
Last Post: searching1
  telnetlib.Telnet timeout issue.. taking much time anna 1 3,198 Jan-19-2018, 01:52 PM
Last Post: devs4p

Forum Jump:

Users browsing this thread: 1 Guest(s)