1

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?

1
  • While it is not your problem, the convention in Python for variable and function names is words lowercase separated by underscores, rather than camel case. (see PEP 8) Commented Mar 31, 2012 at 3:25

1 Answer 1

2

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:

output = output + row[0]
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that really does help clarify thing a bit.

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.