Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

sqlite python searching with 2 and more words as arguments

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

find out.. just input word1%word2 as argument and it process both words in sqlite search


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...