I am a beginner in SQLite, so please bear with me if what i’m about to ask seems silly.
I currently have a database called “Status”, with two columns “stamp” and “messages”. “messages” contains a text, and “stamp” contains a time stamp.
I would like to read the last 10 entries of this database, and display them to a HTML page.
The code I am using to read from database is:
@cherrypy.expose
def readStatus(self):
con = lite.connect('static/database/Status.db')
output = ""
with con:
cur = con.cursor()
cur.execute("SELECT messages FROM Status ORDER BY stamp DESC LIMIT 5")
print "The Status database now contains:"
for row in cur:
print row
output = output + row
return output
The code I am using to print the messages onto an HTML page is
@cherrypy.expose
def homepage(self):
"""Display the home page showing status update
"""
page = get_file(staticfolder+"/html/homePage.html")
page = page.replace("$STATUS", self.readStatus())
return page
However, I am getting this error message:
File "proj1base.py", line 133, in homepage
page = page.replace("$STATUS", self.readStatus())
File "proj1base.py", line 305, in readStatus
output = output + row
TypeError: cannot concatenate 'str' and 'tuple' objects
I’m not entirely sure what this means, I assume it is saying that the type between ‘output’ and ‘row’ is mismatching. However, what ‘row’ should be reading is ‘messages’ column of database ‘status’, which is a string, and ‘output’ is also a string. So I don’t know what’s happening and is wondering if anyone could help?
While you’re querying for only one column, SQLite also supports querying for multiple columns. However, for consistency, Python always puts them in a tuple. You probably want to get the first element of the tuple, which will be a string: