我正在为一家药房开发一个项目,需要用 Python 构建一个 SQLite 数据库,以便根据所治疗的医疗状况(例如头痛、感冒等)快速查询产品库存。产品列表由大约 4000 个项目组成,我想实现一个系统,在该系统中搜索条件也会返回同义条件的产品。例如:
• A search for “flu” should return products for “influenza.”
• A search for “fever” should return products for “high temperature.”
• A search for “stomach ache” should return products for “abdominal pain.”
• A search for “runny nose” should return products for “rhinorrhea.”
我已经编写了创建产品表的基本代码,但我不确定如何构建数据库以有效支持条件同义词查询。
我尝试过的代码:
import sqlite3
# Connect to the database (or create it if it doesn't exist)
conn = sqlite3.connect('pharmacy.db')
cursor = conn.cursor()
# Create product table
cursor.execute('''
CREATE TABLE products (
product_id INTEGER PRIMARY KEY,
name TEXT,
condition TEXT,
quantity_in_stock INTEGER
)
''')
# Insert sample product
cursor.execute('''
INSERT INTO products (name, condition, quantity_in_stock)
VALUES ('Paracetamol', 'headache', 50)
''')
conn.commit()
conn.close()
我的问题是:
• 如何修改数据库结构以有效支持查询条件及其同义词?
我认为条件和同义词表将允许您通过将产品链接到条件,然后将条件与各种同义词链接来处理同义词查找。
产品表:您已经存储产品详细信息的表
条件表:存储医疗条件。
同义词表:存储医疗状况的同义词。
Product_Conditions 表:将产品与一个或多个条件相关联的链接表
import sqlite3
# Connect to the database (or create it if it doesn't exist)
conn = sqlite3.connect('pharmacy.db')
cursor = conn.cursor()
# Create product table
cursor.execute('''
CREATE TABLE IF NOT EXISTS products (
product_id INTEGER PRIMARY KEY,
name TEXT,
quantity_in_stock INTEGER
)
''')
# Create conditions table
cursor.execute('''
CREATE TABLE IF NOT EXISTS conditions (
condition_id INTEGER PRIMARY KEY,
condition_name TEXT UNIQUE
)
''')
# Create synonyms table
cursor.execute('''
CREATE TABLE IF NOT EXISTS synonyms (
synonym_id INTEGER PRIMARY KEY,
synonym_name TEXT UNIQUE,
condition_id INTEGER,
FOREIGN KEY (condition_id) REFERENCES conditions(condition_id)
)
''')
# Create product_conditions table
cursor.execute('''
CREATE TABLE IF NOT EXISTS product_conditions (
product_condition_id INTEGER PRIMARY KEY,
product_id INTEGER,
condition_id INTEGER,
FOREIGN KEY (product_id) REFERENCES products(product_id),
FOREIGN KEY (condition_id) REFERENCES conditions(condition_id)
)
''')
# Insert conditions
cursor.execute("INSERT OR IGNORE INTO conditions (condition_name) VALUES ('flu'), ('headache')")
cursor.execute("INSERT OR IGNORE INTO conditions (condition_name) VALUES ('fever'), ('cold')")
# Insert synonyms
cursor.execute("INSERT OR IGNORE INTO synonyms (synonym_name, condition_id) VALUES ('influenza', 1), ('high temperature', 3)")
# Insert product and associate it with a condition
cursor.execute("INSERT INTO products (name, quantity_in_stock) VALUES ('Paracetamol', 50)")
cursor.execute("INSERT INTO product_conditions (product_id, condition_id) VALUES (1, 2)") # Paracetamol for headache
conn.commit()
conn.close()
搜索示例:
def search_products_by_condition(condition_name):
conn = sqlite3.connect('pharmacy.db')
cursor = conn.cursor()
query = '''
SELECT p.name, p.quantity_in_stock
FROM products p
JOIN product_conditions pc ON p.product_id = pc.product_id
JOIN conditions c ON pc.condition_id = c.condition_id
LEFT JOIN synonyms s ON c.condition_id = s.condition_id
WHERE c.condition_name = ? OR s.synonym_name = ?
'''
cursor.execute(query, (condition_name, condition_name))
results = cursor.fetchall()
conn.close()
return results
# Example usage
results = search_products_by_condition('flu')
for result in results:
print(result)