Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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,))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading