Using a Tkinter Optionbox to carry out a MySQL query












-1














I am trying to use a tkinter option menu to select the variable to search for in a table. A problem arises however as the passed variable does not yield any result.



Here is the table: table



And here is proof that the SQL syntax is not incorrect.
Proof Of SQL



The problem is due to the string variable being incorrect and returning:






no data.



When I select a variable from the OptionMenu, instead of getting:



jhgfds


I get:



('jhgfds',)


So understandably I get no result.



I have tried using these methods on the :




  • Creating a non-tkinter variable (`StrEditEvent)

  • The re method

  • The [2:-3] method


However these have not worked



import tkinter as tk
import mysql.connector
root=tk.Tk()
EventList=

def OptionChanged(*args):
EventSQL=("SELECT * FROM events WHERE eventname=%s")
print(EditEvent.get())
StrEditEvent=EditEvent.get()
print(StrEditEvent)
mycursor.execute(EventSQL,(StrEditEvent,))
myresults=mycursor.fetchall()
print(myresults)

# Adding Tracking Variable EditEvent
EditEvent = tk.StringVar()
EditEvent.trace("w", OptionChanged)
#Connecting To My Database
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="Cranmore1",
database="scoutsdatabase"
)
print(mydb)
mycursor = mydb.cursor()

mycursor.execute("SELECT eventname FROM events")
myresults=mycursor.fetchall()
for i in myresults:
EventList.append(i)
EventToEditOptionMenu = tk.OptionMenu(root,EditEvent,*EventList)
EventToEditOptionMenu.grid(row=1,column=1)


root.mainloop()


Any help would be greatly appreciated.




print(EventList)



[('jhgfds',), ('uytrds',), ('sadfghjk',), ('jhytre',), ('j',), ('h',), ('q',), ('BBC',), ('BBC',), ('qwed',)]










share|improve this question
























  • May I see the output of print(EventList)
    – stovfl
    Nov 22 at 18:57










  • You may: [('jhgfds',), ('uytrds',), ('sadfghjk',), ('jhytre',), ('j',), ('h',), ('q',), ('BBC',), ('BBC',), ('qwed',)]
    – MainStreet
    Nov 22 at 22:46






  • 1




    it returns the result as tuple so unless you iterate over it.
    – AD WAN
    Nov 23 at 6:17






  • 1




    Change to for _tuple in myresults: EventList.append(_tuple[0])
    – stovfl
    Nov 23 at 8:00
















-1














I am trying to use a tkinter option menu to select the variable to search for in a table. A problem arises however as the passed variable does not yield any result.



Here is the table: table



And here is proof that the SQL syntax is not incorrect.
Proof Of SQL



The problem is due to the string variable being incorrect and returning:






no data.



When I select a variable from the OptionMenu, instead of getting:



jhgfds


I get:



('jhgfds',)


So understandably I get no result.



I have tried using these methods on the :




  • Creating a non-tkinter variable (`StrEditEvent)

  • The re method

  • The [2:-3] method


However these have not worked



import tkinter as tk
import mysql.connector
root=tk.Tk()
EventList=

def OptionChanged(*args):
EventSQL=("SELECT * FROM events WHERE eventname=%s")
print(EditEvent.get())
StrEditEvent=EditEvent.get()
print(StrEditEvent)
mycursor.execute(EventSQL,(StrEditEvent,))
myresults=mycursor.fetchall()
print(myresults)

# Adding Tracking Variable EditEvent
EditEvent = tk.StringVar()
EditEvent.trace("w", OptionChanged)
#Connecting To My Database
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="Cranmore1",
database="scoutsdatabase"
)
print(mydb)
mycursor = mydb.cursor()

mycursor.execute("SELECT eventname FROM events")
myresults=mycursor.fetchall()
for i in myresults:
EventList.append(i)
EventToEditOptionMenu = tk.OptionMenu(root,EditEvent,*EventList)
EventToEditOptionMenu.grid(row=1,column=1)


root.mainloop()


Any help would be greatly appreciated.




print(EventList)



[('jhgfds',), ('uytrds',), ('sadfghjk',), ('jhytre',), ('j',), ('h',), ('q',), ('BBC',), ('BBC',), ('qwed',)]










share|improve this question
























  • May I see the output of print(EventList)
    – stovfl
    Nov 22 at 18:57










  • You may: [('jhgfds',), ('uytrds',), ('sadfghjk',), ('jhytre',), ('j',), ('h',), ('q',), ('BBC',), ('BBC',), ('qwed',)]
    – MainStreet
    Nov 22 at 22:46






  • 1




    it returns the result as tuple so unless you iterate over it.
    – AD WAN
    Nov 23 at 6:17






  • 1




    Change to for _tuple in myresults: EventList.append(_tuple[0])
    – stovfl
    Nov 23 at 8:00














-1












-1








-1







I am trying to use a tkinter option menu to select the variable to search for in a table. A problem arises however as the passed variable does not yield any result.



Here is the table: table



And here is proof that the SQL syntax is not incorrect.
Proof Of SQL



The problem is due to the string variable being incorrect and returning:






no data.



When I select a variable from the OptionMenu, instead of getting:



jhgfds


I get:



('jhgfds',)


So understandably I get no result.



I have tried using these methods on the :




  • Creating a non-tkinter variable (`StrEditEvent)

  • The re method

  • The [2:-3] method


However these have not worked



import tkinter as tk
import mysql.connector
root=tk.Tk()
EventList=

def OptionChanged(*args):
EventSQL=("SELECT * FROM events WHERE eventname=%s")
print(EditEvent.get())
StrEditEvent=EditEvent.get()
print(StrEditEvent)
mycursor.execute(EventSQL,(StrEditEvent,))
myresults=mycursor.fetchall()
print(myresults)

# Adding Tracking Variable EditEvent
EditEvent = tk.StringVar()
EditEvent.trace("w", OptionChanged)
#Connecting To My Database
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="Cranmore1",
database="scoutsdatabase"
)
print(mydb)
mycursor = mydb.cursor()

mycursor.execute("SELECT eventname FROM events")
myresults=mycursor.fetchall()
for i in myresults:
EventList.append(i)
EventToEditOptionMenu = tk.OptionMenu(root,EditEvent,*EventList)
EventToEditOptionMenu.grid(row=1,column=1)


root.mainloop()


Any help would be greatly appreciated.




print(EventList)



[('jhgfds',), ('uytrds',), ('sadfghjk',), ('jhytre',), ('j',), ('h',), ('q',), ('BBC',), ('BBC',), ('qwed',)]










share|improve this question















I am trying to use a tkinter option menu to select the variable to search for in a table. A problem arises however as the passed variable does not yield any result.



Here is the table: table



And here is proof that the SQL syntax is not incorrect.
Proof Of SQL



The problem is due to the string variable being incorrect and returning:






no data.



When I select a variable from the OptionMenu, instead of getting:



jhgfds


I get:



('jhgfds',)


So understandably I get no result.



I have tried using these methods on the :




  • Creating a non-tkinter variable (`StrEditEvent)

  • The re method

  • The [2:-3] method


However these have not worked



import tkinter as tk
import mysql.connector
root=tk.Tk()
EventList=

def OptionChanged(*args):
EventSQL=("SELECT * FROM events WHERE eventname=%s")
print(EditEvent.get())
StrEditEvent=EditEvent.get()
print(StrEditEvent)
mycursor.execute(EventSQL,(StrEditEvent,))
myresults=mycursor.fetchall()
print(myresults)

# Adding Tracking Variable EditEvent
EditEvent = tk.StringVar()
EditEvent.trace("w", OptionChanged)
#Connecting To My Database
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="Cranmore1",
database="scoutsdatabase"
)
print(mydb)
mycursor = mydb.cursor()

mycursor.execute("SELECT eventname FROM events")
myresults=mycursor.fetchall()
for i in myresults:
EventList.append(i)
EventToEditOptionMenu = tk.OptionMenu(root,EditEvent,*EventList)
EventToEditOptionMenu.grid(row=1,column=1)


root.mainloop()


Any help would be greatly appreciated.




print(EventList)



[('jhgfds',), ('uytrds',), ('sadfghjk',), ('jhytre',), ('j',), ('h',), ('q',), ('BBC',), ('BBC',), ('qwed',)]







python mysql tkinter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 at 7:53









stovfl

7,3483931




7,3483931










asked Nov 22 at 18:09









MainStreet

295




295












  • May I see the output of print(EventList)
    – stovfl
    Nov 22 at 18:57










  • You may: [('jhgfds',), ('uytrds',), ('sadfghjk',), ('jhytre',), ('j',), ('h',), ('q',), ('BBC',), ('BBC',), ('qwed',)]
    – MainStreet
    Nov 22 at 22:46






  • 1




    it returns the result as tuple so unless you iterate over it.
    – AD WAN
    Nov 23 at 6:17






  • 1




    Change to for _tuple in myresults: EventList.append(_tuple[0])
    – stovfl
    Nov 23 at 8:00


















  • May I see the output of print(EventList)
    – stovfl
    Nov 22 at 18:57










  • You may: [('jhgfds',), ('uytrds',), ('sadfghjk',), ('jhytre',), ('j',), ('h',), ('q',), ('BBC',), ('BBC',), ('qwed',)]
    – MainStreet
    Nov 22 at 22:46






  • 1




    it returns the result as tuple so unless you iterate over it.
    – AD WAN
    Nov 23 at 6:17






  • 1




    Change to for _tuple in myresults: EventList.append(_tuple[0])
    – stovfl
    Nov 23 at 8:00
















May I see the output of print(EventList)
– stovfl
Nov 22 at 18:57




May I see the output of print(EventList)
– stovfl
Nov 22 at 18:57












You may: [('jhgfds',), ('uytrds',), ('sadfghjk',), ('jhytre',), ('j',), ('h',), ('q',), ('BBC',), ('BBC',), ('qwed',)]
– MainStreet
Nov 22 at 22:46




You may: [('jhgfds',), ('uytrds',), ('sadfghjk',), ('jhytre',), ('j',), ('h',), ('q',), ('BBC',), ('BBC',), ('qwed',)]
– MainStreet
Nov 22 at 22:46




1




1




it returns the result as tuple so unless you iterate over it.
– AD WAN
Nov 23 at 6:17




it returns the result as tuple so unless you iterate over it.
– AD WAN
Nov 23 at 6:17




1




1




Change to for _tuple in myresults: EventList.append(_tuple[0])
– stovfl
Nov 23 at 8:00




Change to for _tuple in myresults: EventList.append(_tuple[0])
– stovfl
Nov 23 at 8:00












2 Answers
2






active

oldest

votes


















1














To get the result as jhgfds you need to iterate over it because it returns the result for the query as a tuple [('jhgfds',), ('uytrds',), ('sadfghjk',), ('jhytre',), ('j',), ('h',), ('q',), ('BBC',), ('BBC',), ('qwed',)]



You can use index to get the specific result you want result[0] or result[2]



def OptionChanged(*args):
EventSQL=("SELECT * FROM events WHERE eventname=%s")
print(EditEvent.get())
StrEditEvent=EditEvent.get()
print(StrEditEvent)
mycursor.execute(EventSQL,(StrEditEvent,))
myresults=mycursor.fetchall()

for result in myresults: # iterate over it
print(result)
print(result[2])
print(result[5])





share|improve this answer























  • @MainStreet if the answer solves your problem make sure you tick it as reference for future programmers
    – AD WAN
    Nov 23 at 9:31










  • Unfortunately, your solution does not work as you've neglected to notice that no results were being created in the current state of the program. Thanks for your efforts anyhow.
    – MainStreet
    Nov 23 at 13:58



















0














I've finally worked out the answer! To obtain the string from the tuple, one must use the map() command.



def OptionChanged(*args):
EventSQL=("SELECT * FROM events WHERE eventname=%s")
StrEditEvent=EditEvent.get()
start,mid,end=map(str,StrEditEvent.split("'"))
print(mid)
mycursor.execute(EventSQL,(mid,))
myresults=mycursor.fetchall()
print(myresults)


Thus ('jhgfds',) is converted into jhgfds so the SQL query finds the data from the database.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436289%2fusing-a-tkinter-optionbox-to-carry-out-a-mysql-query%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    To get the result as jhgfds you need to iterate over it because it returns the result for the query as a tuple [('jhgfds',), ('uytrds',), ('sadfghjk',), ('jhytre',), ('j',), ('h',), ('q',), ('BBC',), ('BBC',), ('qwed',)]



    You can use index to get the specific result you want result[0] or result[2]



    def OptionChanged(*args):
    EventSQL=("SELECT * FROM events WHERE eventname=%s")
    print(EditEvent.get())
    StrEditEvent=EditEvent.get()
    print(StrEditEvent)
    mycursor.execute(EventSQL,(StrEditEvent,))
    myresults=mycursor.fetchall()

    for result in myresults: # iterate over it
    print(result)
    print(result[2])
    print(result[5])





    share|improve this answer























    • @MainStreet if the answer solves your problem make sure you tick it as reference for future programmers
      – AD WAN
      Nov 23 at 9:31










    • Unfortunately, your solution does not work as you've neglected to notice that no results were being created in the current state of the program. Thanks for your efforts anyhow.
      – MainStreet
      Nov 23 at 13:58
















    1














    To get the result as jhgfds you need to iterate over it because it returns the result for the query as a tuple [('jhgfds',), ('uytrds',), ('sadfghjk',), ('jhytre',), ('j',), ('h',), ('q',), ('BBC',), ('BBC',), ('qwed',)]



    You can use index to get the specific result you want result[0] or result[2]



    def OptionChanged(*args):
    EventSQL=("SELECT * FROM events WHERE eventname=%s")
    print(EditEvent.get())
    StrEditEvent=EditEvent.get()
    print(StrEditEvent)
    mycursor.execute(EventSQL,(StrEditEvent,))
    myresults=mycursor.fetchall()

    for result in myresults: # iterate over it
    print(result)
    print(result[2])
    print(result[5])





    share|improve this answer























    • @MainStreet if the answer solves your problem make sure you tick it as reference for future programmers
      – AD WAN
      Nov 23 at 9:31










    • Unfortunately, your solution does not work as you've neglected to notice that no results were being created in the current state of the program. Thanks for your efforts anyhow.
      – MainStreet
      Nov 23 at 13:58














    1












    1








    1






    To get the result as jhgfds you need to iterate over it because it returns the result for the query as a tuple [('jhgfds',), ('uytrds',), ('sadfghjk',), ('jhytre',), ('j',), ('h',), ('q',), ('BBC',), ('BBC',), ('qwed',)]



    You can use index to get the specific result you want result[0] or result[2]



    def OptionChanged(*args):
    EventSQL=("SELECT * FROM events WHERE eventname=%s")
    print(EditEvent.get())
    StrEditEvent=EditEvent.get()
    print(StrEditEvent)
    mycursor.execute(EventSQL,(StrEditEvent,))
    myresults=mycursor.fetchall()

    for result in myresults: # iterate over it
    print(result)
    print(result[2])
    print(result[5])





    share|improve this answer














    To get the result as jhgfds you need to iterate over it because it returns the result for the query as a tuple [('jhgfds',), ('uytrds',), ('sadfghjk',), ('jhytre',), ('j',), ('h',), ('q',), ('BBC',), ('BBC',), ('qwed',)]



    You can use index to get the specific result you want result[0] or result[2]



    def OptionChanged(*args):
    EventSQL=("SELECT * FROM events WHERE eventname=%s")
    print(EditEvent.get())
    StrEditEvent=EditEvent.get()
    print(StrEditEvent)
    mycursor.execute(EventSQL,(StrEditEvent,))
    myresults=mycursor.fetchall()

    for result in myresults: # iterate over it
    print(result)
    print(result[2])
    print(result[5])






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 23 at 6:44

























    answered Nov 23 at 6:26









    AD WAN

    8551215




    8551215












    • @MainStreet if the answer solves your problem make sure you tick it as reference for future programmers
      – AD WAN
      Nov 23 at 9:31










    • Unfortunately, your solution does not work as you've neglected to notice that no results were being created in the current state of the program. Thanks for your efforts anyhow.
      – MainStreet
      Nov 23 at 13:58


















    • @MainStreet if the answer solves your problem make sure you tick it as reference for future programmers
      – AD WAN
      Nov 23 at 9:31










    • Unfortunately, your solution does not work as you've neglected to notice that no results were being created in the current state of the program. Thanks for your efforts anyhow.
      – MainStreet
      Nov 23 at 13:58
















    @MainStreet if the answer solves your problem make sure you tick it as reference for future programmers
    – AD WAN
    Nov 23 at 9:31




    @MainStreet if the answer solves your problem make sure you tick it as reference for future programmers
    – AD WAN
    Nov 23 at 9:31












    Unfortunately, your solution does not work as you've neglected to notice that no results were being created in the current state of the program. Thanks for your efforts anyhow.
    – MainStreet
    Nov 23 at 13:58




    Unfortunately, your solution does not work as you've neglected to notice that no results were being created in the current state of the program. Thanks for your efforts anyhow.
    – MainStreet
    Nov 23 at 13:58













    0














    I've finally worked out the answer! To obtain the string from the tuple, one must use the map() command.



    def OptionChanged(*args):
    EventSQL=("SELECT * FROM events WHERE eventname=%s")
    StrEditEvent=EditEvent.get()
    start,mid,end=map(str,StrEditEvent.split("'"))
    print(mid)
    mycursor.execute(EventSQL,(mid,))
    myresults=mycursor.fetchall()
    print(myresults)


    Thus ('jhgfds',) is converted into jhgfds so the SQL query finds the data from the database.






    share|improve this answer


























      0














      I've finally worked out the answer! To obtain the string from the tuple, one must use the map() command.



      def OptionChanged(*args):
      EventSQL=("SELECT * FROM events WHERE eventname=%s")
      StrEditEvent=EditEvent.get()
      start,mid,end=map(str,StrEditEvent.split("'"))
      print(mid)
      mycursor.execute(EventSQL,(mid,))
      myresults=mycursor.fetchall()
      print(myresults)


      Thus ('jhgfds',) is converted into jhgfds so the SQL query finds the data from the database.






      share|improve this answer
























        0












        0








        0






        I've finally worked out the answer! To obtain the string from the tuple, one must use the map() command.



        def OptionChanged(*args):
        EventSQL=("SELECT * FROM events WHERE eventname=%s")
        StrEditEvent=EditEvent.get()
        start,mid,end=map(str,StrEditEvent.split("'"))
        print(mid)
        mycursor.execute(EventSQL,(mid,))
        myresults=mycursor.fetchall()
        print(myresults)


        Thus ('jhgfds',) is converted into jhgfds so the SQL query finds the data from the database.






        share|improve this answer












        I've finally worked out the answer! To obtain the string from the tuple, one must use the map() command.



        def OptionChanged(*args):
        EventSQL=("SELECT * FROM events WHERE eventname=%s")
        StrEditEvent=EditEvent.get()
        start,mid,end=map(str,StrEditEvent.split("'"))
        print(mid)
        mycursor.execute(EventSQL,(mid,))
        myresults=mycursor.fetchall()
        print(myresults)


        Thus ('jhgfds',) is converted into jhgfds so the SQL query finds the data from the database.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 at 14:02









        MainStreet

        295




        295






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436289%2fusing-a-tkinter-optionbox-to-carry-out-a-mysql-query%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            How to ignore python UserWarning in pytest?

            Alexandru Averescu