How can I store HTML in a database (SQLITE PYTHON)

It's probably pretty simple, but I can't get there ...

How can I store html code in SQLITE database?

I use text as a data type for a field in the database (should it be blob?)

I get weird errors (and change erros with the same input, so I think this has something to do with escaping)

MY CODE:

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

Errors:

1 -

OperationalError: next to the syntax "2": error WARNING: File execution error: Python 2.6.5 (r265: 79063, 04.16.2010, 13:09:56) Enter "copyright", "credits" or "license" to obtain additional information.

2-

ProgrammingError: 8- , text_factory, 8- bytestrings ( text_factory = str). , Unicode.

P.s. , ( ), blob, , .

Thanx

+3
1

Try:

cur.execute(
    "UPDATE posts SET html = ? WHERE id = ?", (html ,temp[i][1]))

, sqlite3 . ( SQL-.)

Error: html unicode, string. URL-:

response=urllib2.urlopen(str(temp[i][4]))

:

content_type=response.headers.getheader('Content-Type')
print(content_type)

-

'text/html; charset=utf-8'

html utf-8:

html = response.read().decode('utf-8')

html unicode () ProgrammingError.

+3

Source: https://habr.com/ru/post/1780968/


All Articles