Farzan
Farzan

Reputation: 617

How can I store a string with mixed quotes in a Sqlite database using python?

I have a string with mixed quotes that is " and '. I want to store the string in a Text field in a sqlite3 database using python.

Here is the query I'm using and I have a function that executes these queries.

"""INSERT INTO SNIPPETS (CONTENT, LANGUAGE, TITLE, BACKGROUND)
                        VALUES("{0}" ,"{1}","{2}", "{3}")
            """.format(content, language, title, background)

Something like:

with self.connection as conn:
                cursor = conn.cursor()
                try:
                    result = cursor.execute(statement)
                    if(insert_operation):
                        return cursor.lastrowid

                    return result.fetchall()

Upvotes: 0

Views: 211

Answers (1)

Hai Vu
Hai Vu

Reputation: 40688

You don't have to make it too complicated. Python sqlite3 will take care of the quoting for you.

statement = 'INSERT INTO SNIPPETS (CONTENT, LANGUAGE, TITLE, BACKGROUND) VALUES (?, ?, ?, ?)'
cursor.execute(statement, (content, language, title, background))

Upvotes: 4

Related Questions