Home > Software design >  How to create SQLite query to insert a string that contains both " and '?
How to create SQLite query to insert a string that contains both " and '?

Time:04-15

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.

CodePudding user response:

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,))
  • Related