Inserting HTML files as strings in database

Gerhard Häring gh at
Tue Apr 15 14:10:45 CEST 2003

vivek kumar <gupt_vive at> wrote:
> hi all,

> I am trying to make a search engine in python. I want to save the html
> files retrieved by the crawler into a Mysql database as strings
> (MEDIUMTEXT). But the characters like ' " etc are giving problems while
> inserting it as a string. [...]

It's generally a bad idea to insert the values into the SQL string
yourself. This, and quoting the values appropriately, is the job of the
DB-API module:

a_really_ugly_string = "".join([chr(x) for x in range(1,255)])

# cx is the connection object
cu = cx.cursor()
    """, (a_really_ugly_string,))

For inserting binary data (i. e. data containing the null byte) you'll need
a binary column type.

-- Gerhard

More information about the Python-list mailing list