Python Forum
SQLite Query multiple column search
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SQLite Query multiple column search
#1
Hi Guys,

As per my previous post, I am new to Python (Gui) and in the process of learning.
I am having a particular issue with a search query against SQLite Db.

What am I trying to achieve?
I have defined three search entries where I am trying to query a table and 2 columns. or Better yet, I would like to query the entire table 'assets2' but when I select * from assets2 it comes up with an argument error.

I have tried the following
AND statement:
cursor.execute("SELECT * FROM `assets2` WHERE `Asset_Serial` AND `Asset_Model` LIKE ?", ('%'+str(SEARCH2.get())+'%',))
OR statement:
cursor.execute("SELECT * FROM `assets2` WHERE `Asset_Serial` or `Asset_Model` LIKE ?", ('%'+str(SEARCH2.get())+'%',))
Below is the search2 snippet.

def Search2():
    if SEARCH2.get() != "":
        tree.delete(*tree.get_children())
        Database()
        cursor.execute("SELECT * FROM `assets2` WHERE `Asset_Serial` LIKE ?", ('%'+str(SEARCH2.get())+'%',))
        fetch = cursor.fetchall()
        for data in fetch:
            tree.insert('', 'end', values=(data))
        cursor.close()
        conn.close()
Thanks Whacky7
Reply
#2
Are you assuming the like operator is applying to Asset_Serial and Asset_Model? Because it only applies to Asset_Model. It's the SQL version of this problem.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Hi ichabod801,

Thanks for your reply. I am not sure on which other operator I can use in order to search across all columns within assets2? or as you mentioned, fine to search across both columns 'asset_serial' and 'asset_model'.

I am using SQLite3.
Reply
#4
You misunderstand. You need to apply the like operator twice, once to each column.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Oh, I See. I am getting stuck with syntax errors and not quite sure how to include the operator twice within the statement (cursor.execute)
Reply
#6
SELECT * FROM assets2 WHERE Asset_Serial LIKE ? or Asset_Model LIKE ?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
Ichabod801,

Thanks so much mate. That worked! I was making errors on the following line of code:

('%'+str(SEARCH2.get())+'%',))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 728 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Search for multiple unknown 3 (2) Byte combinations in a file. lastyle 7 1,321 Aug-14-2023, 02:28 AM
Last Post: deanhystad
  python sqlite autoincrement in primary column janeik 6 1,140 Aug-13-2023, 11:22 AM
Last Post: janeik
  Python: re.findall to find multiple instances don't work but search worked Secret 1 1,208 Aug-30-2022, 08:40 PM
Last Post: deanhystad
  How to combine multiple column values into 1? cubangt 15 2,812 Aug-11-2022, 08:25 PM
Last Post: cubangt
  Reshaping a single column in to multiple column using Python sahar 7 2,042 Jun-20-2022, 12:35 PM
Last Post: deanhystad
  [Solved]Help with search statement-SQLite & Python Extra 1 1,050 May-06-2022, 07:38 PM
Last Post: Extra
  df column aggregate and group by multiple columns SriRajesh 0 1,037 May-06-2022, 02:26 PM
Last Post: SriRajesh
  Building SQLite Query OogieM 10 2,940 Mar-16-2022, 11:59 AM
Last Post: OogieM
  Search multiple CSV files for a string or strings cubangt 7 8,004 Feb-23-2022, 12:53 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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