breaking a loop

E

el chupacabra

Hi, I'm just learning Python....thanks 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/ =-
 
W

wittempj

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('Prompt: ')
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>
 
I

infidel

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

def search():
user_input = ''
while True:
user_input = raw_input('Search 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 :)
 
M

Mosti El

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

el chupacabra said:
Hi, I'm just learning Python....thanks 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]
'+------------------------------------------------------------------+'
#end for statement, end of search


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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,192
Latest member
KalaReid2

Latest Threads

Top