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 select a thing that starts with X letter?

Alright, so basically, i have a table structured like this

ActorID Name Surname Birthdate Website

(with some data in it, of course)

And i want to select an actor depending on what their last name starts with.
So i know the SQL Request should be

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

SELECT ActorID FROM Actors WHERE Name LIKE '{Letter}%'

However, i want to do it with sqlite3 in python, and when i write it this way:

 cur.execute("""
                    SELECT name, surname FROM acteurs WHERE name LIKE '?%';
                
                    """,firstletter)
actors = cur.fetchall()
        

I get a syntax error "sqlite3.OperationalError: near ";": syntax error"

How should i modify it to make it work ?
Thanks in advance

>Solution :

The ? placeholder must not be inside the single quotes.
The % wildcard must be concatenated to the ? placeholder.
Also, you must pass firstletter as a member of a tuple:

cur.execute("SELECT name, surname FROM acteurs WHERE name LIKE ? || '%'", (firstletter,))
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