From d50f244dd68b29d8a71c100a48c2ac289c9172aa Mon Sep 17 00:00:00 2001 From: q Date: Mon, 11 Mar 2013 19:30:01 +0200 Subject: [PATCH] multiple strings in search --- file_list.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/file_list.py b/file_list.py index 4a9d77f..6d76405 100755 --- a/file_list.py +++ b/file_list.py @@ -32,7 +32,7 @@ def setup_options(): help="Exclude folder name from the lists. This option may be issued several times") parser.add_argument("-f",action="store",dest="sqlfile",default=SQLFILE, help="SQL file name to use [%(default)s]") - parser.add_argument("-s",type=str,dest="search",default=False, + parser.add_argument("-s",type=str,action='append',dest="search",default=[], help="Search list based on path pattern") parser.add_argument("--match",type=str,dest="match",default=False, help="Search for closest match from basenames, can be helped with adding -s") @@ -170,10 +170,12 @@ def find_duplicates(sqlfile): return duphash def searchdb(sqlfile,needle): + needle=['%'+i+'%' for i in needle] + like_query=' OR '.join(['file LIKE ?' for i in needle]) conn=sqlite3.connect(sqlfile) conn.text_factory=str db=conn.cursor() - db.execute("SELECT file FROM list WHERE file LIKE ? ORDER BY file",('%'+needle+'%',)) + db.execute("SELECT file FROM list WHERE "+like_query+" ORDER BY file",needle) for row in db: print(row[0]) @@ -182,8 +184,10 @@ def matchdb(sqlfile,needle,helper): conn=sqlite3.connect(sqlfile) conn.text_factory=str db=conn.cursor() - if helper: - db.execute("SELECT file FROM list WHERE file LIKE ?",('%'+needle+'%',)) + if len(helper)>0: + helper=['%'+i+'%' for i in helper] + like_query=' OR '.join(['file LIKE ?' for i in helper]) + db.execute("SELECT file FROM list WHERE "+like_query,helper) else: db.execute("SELECT file FROM list") ratio=0 @@ -210,7 +214,7 @@ def main(): if not os.path.exists(options.sqlfile): createdb(options.sqlfile); - if options.search and not options.match: + if len(options.search)>0 and not options.match: searchdb(options.sqlfile,options.search) sys.exit(0) if options.match: