mardi 29 mars 2016

escape a single quote from a downloaded value sqlite3

I am saving locations on a web page to an sqlite3 database using python/selenium and sqlite3. Some of the locations contain a single quote

For example maryville, John's lane, London

I know that when creating a data base locally, I must use two single quotes to escape this. John''s lane. REF:- questions/603572/how-to-properly-escape-a-single-quote-for-a-sqlite-database How can this be achieved when scraping a website.

My Code is below:-

# get locations
locs = browser.find_elements_by_class_name("meta")
for loc in locs:
    if loc.text !="":
        print loc.text
        query += ",\'"+loc.text.replace(', ','-')+"\'"

I get this error because of the presence of the 's

cur.execute("INSERT INTO LOCATIONS VALUES("+query+");")
sqlite3.OperationalError: near "s": syntax error

I am saving the full address to one fields. Thanks in advance for your help.

Aucun commentaire:

Enregistrer un commentaire