'How to create SQLite query to insert a string that contains both " and '?

I have to create an SQLite query to insert a string that contains both quotes and apostrophes. For example something like this

row = """Cristina O'Brien "Valenzuela" """
query = f"""INSERT INTO Actors (Actor)
            VALUES("{row}")"""

conn.execute(query)

But I have an error

sqlite3.OperationalError: near "Valenzuela": syntax error

I understand that for SQL this string ends before Valenzuela but I have no idea how to deal with it.



Solution 1:[1]

The answer is to let the library do the quoting.

row = """Cristina O'Brien "Valenzuela" """
query = "INSERT INTO Actors (Actor) VALUES (?);"

conn.execute(query, (row,))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim Roberts