Python Practice › SQLite Programs › Program 16

Search with LIKE

EasySQLite Programs · 16 of 20

Write a Python program to find products whose name contains a word.

Advertisement
Output
[('USB cable',), ('USB hub',)]

Approach

  1. LIKE '%' || ? || '%' keeps the value as a parameter.

Solution

Python
import sqlite3 conn = sqlite3.connect(":memory:")conn.execute("CREATE TABLE products (name TEXT)")conn.executemany("INSERT INTO products VALUES (?)", [("USB cable",), ("Wireless mouse",), ("USB hub",), ("Keyboard",)])term = "usb"print(conn.execute("SELECT name FROM products WHERE name LIKE '%' || ? || '%'", (term,)).fetchall())
Output
[('USB cable',), ('USB hub',)]

Line by line

Line 6

SQLite’s LIKE ignores case for English letters.

Learn the concept

New to this topic? Read the lesson, then come back.

More sqlite programs

← All 20 SQLite Programs

Leave a Comment