[Tutor] Searching for email id in MySQL giving wrong results
John Joseph
jjk_saji at yahoo.com
Sun Jan 29 12:05:08 CET 2006
--- Danny Yoo <dyoo at hkn.eecs.berkeley.edu> wrote:
>
> Hi John,
>
> Ok; because the question you're asking looks a
> little homework-y, we have
> to be a little bit more cautious in our answers.
> What part are you
> getting stuck on?
>
>
> Do you understand how SQL wildcards work? There's a
> good number of SQL
> tutorials on the web. For example, we can take a
> look at the queries
> section on Phil Greenspuns "SQL for Web Nerds" page:
>
> http://philip.greenspun.com/sql/queries.html
>
> (In fact, the examples he uses to talk about
> wildcards there seems very
> pertinant to your question!)
>
>
> Or are you getting stuck about constructing such a
> query using string
> operations? If so, you might find something like:
>
>
>
http://diveintopython.org/native_data_types/formatting_strings.html
>
> useful.
>
>
> If you can give us more details on where you're
> having difficulty, we'll
> try to point you in a good direction.
>
>
Hi Danny
Thanks for the link , I understood strings
formating and use of “%s”
Also I read the link for basic SQL statements
Now after this , I am getting lost when I do the
python program
let me try to explain my experience
1>From MySQL command mode , the command to check all
the email id which starts with “j” is
select * from contact where email_id like 'j%';
2>Now when I want to select command in python I get
confused , since I want to use SELECT statement to
display the contacts which email id begins or
contains the searched strings
My script I am adding , Guidance requested
Thanks
Joseph John
*************************************************************************************
import MySQLdb
s_email = raw_input("Enter the Email to be searched
")
db = MySQLdb.connect(host="localhost",user = "john",
passwd = "asdlkj", db = 'learnpython')
entry = db.cursor()
""" I get confused here , in the SQL statement we use
%s for the searched string ,
here I also need to use another % for pattern
matching , I get lost over here
"""
entry.execute("""SELECT * FROM contact WHERE email_id
= %s """, (s_email,))
#entry.execute("""SELECT * FROM contact WHERE email_id
like %s% """, (s_email,))
p = entry.fetchall()
print p
___________________________________________________________
Yahoo! Photos NEW, now offering a quality print service from just 8p a photo http://uk.photos.yahoo.com
More information about the Tutor
mailing list