Skip to content

Commit

Permalink
Use named parameter the right way, limit results to 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Bouni committed Aug 2, 2024
1 parent 204dcdb commit abc3891
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions library.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,10 @@ def get_part_details(self, lcsc: list) -> dict:
con.row_factory = dict_factory
cur = con.cursor()
results = []
query = '''SELECT "LCSC Part" AS lcsc, "Stock" AS stock, "Library Type" AS type FROM parts WHERE parts MATCH ?'''

query = '''SELECT "LCSC Part" AS lcsc, "Stock" AS stock, "Library Type" AS type FROM parts WHERE parts MATCH :number LIMIT 1'''
# Use parameter binding to prevent SQL injection and handle the query more efficiently
for number in lcsc:
# Each number needs to be wrapped in double quotes for exact match in FTS5
match_query = f'"LCSC Part:{number}"'
cur.execute(query, (match_query,))
cur.execute(query, {"number": number})
results.extend(cur.fetchall())
if results:
return results[0]
Expand Down

0 comments on commit abc3891

Please sign in to comment.