Inserting HTML files as strings in database

Gerhard Häring gh at ghaering.de
Tue Apr 15 14:22:57 CEST 2003


vivek kumar <gupt_vive at hotmail.com> 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:

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

# cx is the connection object
cu = cx.cursor()
cu.execute("""
    INSERT INTO MYTABLE(FOO) VALUES (%s)
    """, (a_really_ugly_string,))
cu.commit()
#v-

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