这可能很简单,但我到不了那里。。。在
如何在SQLITE数据库中存储html代码?在
我使用文本作为数据库中字段的数据类型(应该是blob??)在
我遇到了一些奇怪的错误(并且用相同的输入更改错误,所以我认为这与转义有关)
我的代码:con = sqlite.connect(bd)
cur = con.cursor()
temp=cur.execute ('SELECT * from posts').fetchall()
#temp[Z][1] = ID
#temp[Z][4] = URL
i=0
while i< len (temp):
if temp[i][0]==None:
try:
html = urllib2.urlopen(str(temp[i][4])).read()
except:
html=None
#sql = 'UPDATE posts SET html = "' + str(html) + '" WHERE id = ' + str(temp[i][1])
#cur.execute( 'UPDATE posts SET html = ? WHERE id = ?' ,(html,temp[i][1]) )
cur.execute("UPDATE posts SET html = '" + str(html) + "' WHERE id = " + str(temp[i][1]))
con.commit()
print temp[i][4]
i=i+1
错误:
1-OperationalError: near "2": syntax
error WARNING: Failure executing file:
Python 2.6.5
(r265:79063, Apr 16 2010, 13:09:56)
Type "copyright", "credits" or
"license" for more information.
2-ProgrammingError: You must not use
8-bit bytestrings unless you use a
text_factory that can interpret 8-bit
bytestrings (like text_factory = str).
It is highly recommended that you
instead just switch your application
to Unicode strings.
另外,我更希望它是文本(人类可读)而不是blob,但如果这是更简单的方式,我完全赞成。在
塔克斯