breaking a loop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • el chupacabra

    #1

    breaking a loop

    Hi, I'm just learning Python....thank s in advance...

    Do you get out of this loop?

    Problem: When I type 'exit' (no quotes) the program doesn't quit the loop...it actually attemps to find entries that containt the 'exit' string.

    Desired behavior: when I type 'exit' the program should quit.

    def search():
    searchWhat = ""
    while searchWhat != 'exit':
    searchWhat = "%%%s%%" % raw_input ('Search for : ')
    cursor.execute( "select * from TABLE where FIELD like %s", (searchWhat))
    result = cursor.fetchall ()
    print '+------------------------------------------------------------------+'
    print '| # | Name | LastName |'
    print '+------------------------------------------------------------------+'
    for record in result:
    print ' ', record[0], ' : ', record[1], ' ==> ', record[2]
    print '+------------------------------------------------------------------+'
    #end for statement, end of search


    --------------= Posted using GrabIt =----------------
    ------= Binary Usenet downloading made easy =---------
    -= Get GrabIt for free from http://www.shemes.com/ =-

  • wittempj@hotmail.com

    #2
    Re: breaking a loop

    What you do is asking for a string, and then embed the result in '%'
    characters, as this is what you do in the sql statement (btw: where is
    the cursor defined?)

    It is probably a better idea to construct the sql statement with the
    direct result of raw_input, instead of format it straight away - see
    below for the idea

    martin@ubuntu:~ $ python
    Python 2.4.1 (#2, Mar 30 2005, 21:51:10)
    [GCC 3.3.5 (Debian 1:3.3.5-8ubuntu2)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.
    py> s = raw_input('Prom pt: ')
    Prompt: exit
    py> print s
    exit
    py> if s == 'exit':
    .... print 'yes'
    ....
    yes
    py> t = '%%%s%%' %s
    py> print t
    %exit%
    py> sql = "select * from TABLE where FIELD like %s", (s)
    py> print sql
    ('select * from TABLE where FIELD like %s', 'exit')
    py> sql = "select * from TABLE where FIELD like '%%%s%%" % s
    py> print sql
    select * from TABLE where FIELD like '%exit%
    py>

    Comment

    • infidel

      #3
      Re: breaking a loop

      > Desired behavior: when I type 'exit' the program should quit.

      def search():
      user_input = ''
      while True:
      user_input = raw_input('Sear ch for: ')
      if user_input == 'exit': break
      print 'processing', user_input

      Note that putting user input directly into SQL statements leaves your
      database open to SQL-injection attacks :-)

      Comment

      • Mosti El

        #4
        Re: breaking a loop

        I think u need break before exit()
        so if u want break from any loop just add break

        "el chupacabra" <nowayjose@nowa y.com> wrote in message
        news:fUNKe.5188 $5g.4209@tornad o.socal.rr.com. ..[color=blue]
        > Hi, I'm just learning Python....thank s in advance...
        >
        > Do you get out of this loop?
        >
        > Problem: When I type 'exit' (no quotes) the program doesn't quit the[/color]
        loop...it actually attemps to find entries that containt the 'exit' string.[color=blue]
        >
        > Desired behavior: when I type 'exit' the program should quit.
        >
        > def search():
        > searchWhat = ""
        > while searchWhat != 'exit':
        > searchWhat = "%%%s%%" % raw_input ('Search for : ')
        > cursor.execute( "select * from TABLE where FIELD like %s",[/color]
        (searchWhat))[color=blue]
        > result = cursor.fetchall ()
        > print[/color]
        '+------------------------------------------------------------------+'[color=blue]
        > print '| # | Name |[/color]
        LastName |'[color=blue]
        > print[/color]
        '+------------------------------------------------------------------+'[color=blue]
        > for record in result:
        > print ' ', record[0], ' : ', record[1], ' ==> ',[/color]
        record[2][color=blue]
        > print[/color]
        '+------------------------------------------------------------------+'[color=blue]
        > #end for statement, end of search
        >
        >
        > --------------= Posted using GrabIt =----------------
        > ------= Binary Usenet downloading made easy =---------
        > -= Get GrabIt for free from http://www.shemes.com/ =-
        >[/color]


        Comment

        Working...