【问题标题】:In python, what is a character buffer?在python中,什么是字符缓冲区?
【发布时间】:2012-03-31 07:11:54
【问题描述】:

我是SQLite初学者,遇到了一些麻烦,希望能找到可以帮助的人。

我正在尝试从数据库中读取一些数据,将其放入 python 中的变量中,然后将其打印到 HTML 页面上。

数据库中的表名为“Status”,它包含“stamp”和“messages”两列。 "stamp 是一个包含时间戳的 INT,而 "messages" 包含一个 TEXT。

@cherrypy.expose
def comment(self, ID = None):
    con = lite.connect('static/database/Status.db')
    output = ""
    with con:    
        cur = con.cursor()    
        cur.execute("SELECT * FROM Status WHERE stamp = ?", (ID,))
        temp = cur.fetchone()
        output = temp[0]

    comments = self.readComments(ID)


    page = get_file(staticfolder+"/html/commentPage.html")
    page = page.replace("$Status", output)

我得到的错误是:

Traceback (most recent call last):
  File "/usr/lib/pymodules/python2.7/cherrypy/_cprequest.py", line 606, in respond
    cherrypy.response.body = self.handler()
  File "/usr/lib/pymodules/python2.7/cherrypy/_cpdispatch.py", line 25, in __call__
    return self.callable(*self.args, **self.kwargs)
  File "proj1base.py", line 184, in comment
    page = page.replace("$Status", output)
TypeError: expected a character buffer object

我想知道是否有人可以帮助我澄清什么是字符缓冲区对象,以及如何使用它来使我的代码正常工作?

【问题讨论】:

    标签: python html database sqlite


    【解决方案1】:

    将“字符缓冲区”替换为“字符串”作为初学者。 (有更多类型在 Python 中公开“缓冲区协议”,但现在不要打扰它们。)output 很可能最终不是字符串。在错误前的行中记录其类型。

    【讨论】:

    • 谢谢,我知道它是 INT 类型,这意味着它被分配了 col“stamp”而不是“messages”。一旦我解决了这个问题,一切都很好。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-13
    • 2012-07-13
    • 2020-04-24
    • 2020-06-28
    • 2012-06-21
    • 2010-10-13
    相关资源
    最近更新 更多