hello I need to search in sqlite.db using python - my code works BUT for one word as input.. but I would like to search for 2 words for example so to find any item which contains BOTH words.. word1 && word2 doesn't work for me as input, thank you for helping with my code (works with one word as argument input)
import sqlite3
from sqlite3 import Error
#import argparse
import sys
def create_connection(db_file):
""" create a database connection to the SQLite database
specified by the db_file
:param db_file: database file
:return: Connection object or None
"""
conn = None
try:
conn = sqlite3.connect(db_file)
except Error as e:
print(e)
return conn
wordstosearch=sys.argv[1]
print("Looking for:")
print(wordstosearch)
#parser = argparse.ArgumentParser()
#print(parser)
database = r"....path to library.db"
#partNumber partDescription
# create a database connection
returnedItems = 0
conn = create_connection(database)
with conn:
cur = conn.cursor()
cur.execute('SELECT * FROM parts WHERE partNumber like "%{}%" or partDescription like "%{}%"'.format(wordstosearch,wordstosearch))
rows = cur.fetchall()
for row in rows:
print(row)
print("
")
returnedItems = returnedItems + 1
print (returnedItems)
question from:
https://stackoverflow.com/questions/66048622/sqlite-python-searching-with-2-and-more-words-as-arguments 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…