Python Forum
format String in SQL statement.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
format String in SQL statement.
#10
With this:

WHERE last_name = ? AND rental_rate LIKE ? AND rental_date LIKE ? AND return_date LIKE ? AND rental_duration LIKE ?'
   test = db.execute(query,['%'+name+'%','%%','%'+month+'%','%%','%r%'])
you are in effect:
  • checking for an exact match between the name and  '%JohnDoe%' (because the '%' are taken literally since you are using = and not LIKE
  • accepting any rental rate (assuming rental_rate is a string and not a number)
  • checking only for the month in the rental date (and the way it is written it can match any two digits in the date, whether they represent the year, the month, the day, or even digits between them).
  • taking anything for the return date
  • checking that the rental_duration contains a 'r'  

Make it simple for yourself, just make queries that take only one single criterion (name, rate,dates, duration...), and test them individually. Once you have them all working
combine them in one single query.

In addition, people rarely come up with SQL queries directly in their application code. They use SQL editors, that allow them to test their queries directly, show them error in the SQL synatx, display results, etc... . Once these queries work, they are adapted to be part of the code of the application (which at that point is a fairly trivial task). A free SQL editor is Squirrel-SQL.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Messages In This Thread
format String in SQL statement. - by Steven - May-26-2017, 03:44 AM
RE: format String in SQL statement. - by Ofnuts - May-26-2017, 05:15 PM
RE: format String in SQL statement. - by Steven - May-26-2017, 11:12 PM
RE: format String in SQL statement. - by Ofnuts - May-27-2017, 08:17 AM
RE: format String in SQL statement. - by Steven - May-27-2017, 06:16 PM
RE: format String in SQL statement. - by Ofnuts - May-27-2017, 07:50 PM
RE: format String in SQL statement. More problems. - by Ofnuts - May-29-2017, 08:41 PM
RE: format String in SQL statement. - by buran - May-29-2017, 08:33 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trouble with assigning a string value in conditional statement juliabrushett 1 2,584 Jun-16-2018, 06:21 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020