Python Forum
mysql connector/telnet issue (re: text game)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
mysql connector/telnet issue (re: text game)
#1
"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!"
Reply
#2
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,))

:)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  telnet from ssh tunnel oldfart 3 7,063 Jul-17-2020, 02:28 PM
Last Post: noobami99
  telnet to a device under tacacs management kang18 0 1,540 Jun-05-2020, 06:11 AM
Last Post: kang18
  [mysql.connector] The database is never connected to and there are no errors SheeppOSU 7 4,031 Jun-04-2020, 09:47 PM
Last Post: SheeppOSU
  3.6 telnet eyler 3 11,209 Jun-28-2019, 05:22 AM
Last Post: Khanhamid90
  Any suggestion on python library to use for both ssh and telnet? lord_mani 4 3,667 Jun-25-2019, 04:07 PM
Last Post: gb74razor
  telnet question jacklee26 2 2,459 Mar-30-2019, 06:45 AM
Last Post: jacklee26
  Retrieve output from telnet command Networker 1 4,041 Mar-12-2019, 01:36 PM
Last Post: searching1
  Issue when running telnet program using python3 searching1 11 13,366 Dec-22-2018, 12:48 AM
Last Post: searching1
  Aggregate multiple telnet connections Jibeji 1 4,220 Mar-02-2018, 07:21 PM
Last Post: mpd
  Multithread telnet not working Parallel anna 7 7,354 Feb-05-2018, 01:17 PM
Last Post: anna

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020