Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SQL wild card use
#1
I am trying to use a " * " in a a SQL WHERE clause passed in as an argument. The reason is to return the last entered tagNo if they do not know the other two search criteria.
I get no returns with the use of the '*' but do when I pass he exact match for all three WHERE criteria. Can you use wild cards the way i am trying to use them?

            tagNo=self.cow_detBtn['tag'].text
            tagClr=self.cow_detBtn['tagClr'].text
            tagYr=self.cow_detBtn['tagYr'].text
            if tagNo!='' and tagClr!='' and tagYr!='':
                cow=self.db.searchCow(tagNo,tagClr,tagYr)
            elif tagNo!='' and tagClr!='' and tagYr=='':
                cow=self.db.searchCow(tagNo,tagClr,'*')
            elif tagNo!='' and tagClr=='' and tagYr!='':
                cow=self.db.searchCow(tagNo,'*',tagYr)
            elif tagNo!='' and tagClr=='' and tagYr=='':
                cow=self.db.searchCow(tagNo,'*','*')

def searchCow(self,tagNo,tagClr,tagYr):
        self.c.execute('SELECT * From cowTbl where tagNo=? and tagClr=? and tagYr=? LIMIT 1',(tagNo,tagClr,tagYr))
        cows=self.c.fetchall()
        return cows
Reply
#2
May could do something like:

def searchCow(self,tagNo,tagClr,tagYr):
    if tagNo and tagClr and tagYr:
        # All args are true, pull this record
        self.c.execute('SELECT * From cowTbl where tagNo=? and tagClr=? and tagYr=? LIMIT 1',(tagNo,tagClr,tagYr))
    elif tagNo or tagClr or tagYr:
        # Atleast one arg is true, pull this recored
        self.c.execute('select * from cowTbl where tagNo=? or tagClr=? or tagYr=? limit 1',(tagNo,tagClr,tagYr))
    else:
        # All args are empty pull the last record entered
        self.c.execute('select * from cowTbl order by tagNo desc limit 1')
    cows=self.c.fetchall()
    return cows
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
Wildcard characters in SQL are underscore ( _ ) for one character and percent ( % ) for zero or more characters. BUT when you use a wildcard character you must not use de equals ( = ) operator, but the "LIKE" operator.
select * from cowTbl where tagClr LIKE 'blac%);
This only goes for character fields, not numbers.
Reply
#4
Thanks all.
Gives me something to work with.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Having strange results from an RFID HID card reader - I'm stuck orbisnz 1 1,502 Mar-28-2022, 08:20 AM
Last Post: Larz60+
  Credit card number redacting script Drone4four 6 5,221 Jan-18-2019, 02:07 PM
Last Post: Drone4four
  Validating credit card frequency 8 4,208 Nov-05-2018, 07:36 PM
Last Post: frequency
  individual's ID card number in python danpek 2 3,779 Jun-14-2018, 04:07 PM
Last Post: DeaD_EyE
  RAW IO disk access for SD Card shift838 1 8,591 Feb-27-2017, 03:35 AM
Last Post: Larz60+
  "Card Dealing" Python code 2.7 Paradoxalis 3 6,636 Nov-17-2016, 11:32 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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