Bob Cat
Bob Cat

Reputation: 15

displaying results from sqlite database query

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?

Upvotes: 1

Views: 6089

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1123480

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>'

Upvotes: 2

Related Questions