1

I have created a CGI webpage with a search box. My script queries an sqlite database with the input from the search box.

Now, I want to display (on my webpage) all columns of the rows that matched the search, but when I do the following:

query = raw_input(desc)
query = '%' + query + '%'
cursor.execute("SELECT * from Table WHERE Column LIKE ?", (query,))
print cursor.fetchall

the webpage only displays the query itself - no results from the database.

Any ideas how I can get the results to display?

1
  • You should actually call fetchall and not only reference it. Commented Dec 19, 2013 at 23:45

1 Answer 1

2

You want to call cursor.fetchall(); you are merely displaying the representation of the method otherwise:

print cursor.fetchall()

The reason you are not seeing anything is because the representation of a method object in Python uses angle brackets, like a HTML tag:

>>> print c.fetchall
<built-in method fetchall of sqlite3.Cursor object at 0x11059d9d0>

Your browser doesn't know anything about a <built-in> HTML tag and just doesn't display it.

You may want to create an actual table with the results:

cursor.execute("SELECT * from Table WHERE Column LIKE ?", (query,))
print '<table>'

for row in cursor:
    print '<tr>{}</tr>'.format(''.join(['<td>{}</td>'.format(col) for col in row]))

print '</table>'
Sign up to request clarification or add additional context in comments.

6 Comments

It would be great if I could create this table! But I tried replacing my fetchall command with the table commands you suggested, and I am still getting the query itself in the display.
Neither the code in your question nor my answer would ever display the SQL query, unless you mean the whole script source code is being displayed. Do any changes in the script show in the browser? Are you certain the right file is being altered here?
I think I used the wrong term. It's displaying whatever was input into the search box on the site. So if I type "Gene" into the box, the results show "Gene", and nothing else.
Right, but nowhere in this or your code is query printed. If you added print "Hello!", do you see that in your browser? If not, then this script is not what is being run.
Yes, print "Hello!" prints Hello! to the browser.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.