"""Run in an empty directory: recipe search, storage, and escaping examples."""
import sqlite3

con = sqlite3.connect(':memory:')
con.execute('CREATE TABLE recipes (id INTEGER PRIMARY KEY, title TEXT, note TEXT)')
con.execute("""
    INSERT INTO recipes VALUES
        (1, 'California burrito', 'Carne asada & fries'),
        (2, 'Breakfast burrito', 'Add eggs & cheese')
""")
con.commit()

rows = con.execute('SELECT id, title FROM recipes ORDER BY id').fetchall()
print(rows)  # [(1, 'California burrito'), (2, 'Breakfast burrito')]

cur = con.execute('SELECT title FROM recipes ORDER BY id')
print(cur.fetchall())  # [('California burrito',), ('Breakfast burrito',)]
rows = cur.fetchall()
print(rows)  # []

rows = con.execute('SELECT title FROM recipes ORDER BY id').fetchall()
print(rows)
titles = [row[0] for row in rows]

db = sqlite3.connect('recipe_notes.db')
db.execute('CREATE TABLE notes (text TEXT)')
db.execute("INSERT INTO notes VALUES ('Add salsa')")
db.commit()
db.execute("UPDATE notes SET text = 'Add guacamole'")
print(db.execute('SELECT text FROM notes').fetchone()[0])  # Add guacamole
db.close()

db = sqlite3.connect('recipe_notes.db')
print(db.execute('SELECT text FROM notes').fetchone()[0])  # Add salsa
db.close()

title = "California burrito' OR title='Breakfast burrito"
sql = "SELECT title FROM recipes WHERE title='" + title + "' ORDER BY id"
print(sql)
# SELECT title FROM recipes WHERE title='California burrito' OR title='Breakfast burrito' ORDER BY id
print(con.execute(sql).fetchall())  # [('California burrito',), ('Breakfast burrito',)]

title = "California burrito'--"
sql = "SELECT title FROM recipes WHERE title='" + title + "' AND id > 1"
print(sql)
# SELECT title FROM recipes WHERE title='California burrito'--' AND id > 1
print(con.execute(sql).fetchall())  # [('California burrito',)]

db = sqlite3.connect(':memory:')
db.execute('CREATE TABLE suggestions (title TEXT, note TEXT)')
title = "Burrito', 'Try it'), ('Taco"
note = 'Try it'
sql = "INSERT INTO suggestions VALUES ('" + title + "', '" + note + "')"
print(sql)
# INSERT INTO suggestions VALUES ('Burrito', 'Try it'), ('Taco', 'Try it')
db.execute(sql)
print(db.execute('SELECT count(*) FROM suggestions').fetchone()[0])  # 2

def find_titles(title):
    rows = con.execute(
        'SELECT title FROM recipes WHERE title = ? ORDER BY id',
        (title,),
    ).fetchall()
    return [row[0] for row in rows]

print(find_titles('California burrito'))  # ['California burrito']
print(find_titles("California burrito' OR title='Breakfast burrito"))  # []

title = "Chef's burrito"
note = 'Toast the tortilla'
con.execute('INSERT INTO recipes (title, note) VALUES (?, ?)', (title, note))
con.commit()
print(find_titles(title))  # ["Chef's burrito"]

pattern = 'B%'
print(con.execute('SELECT title FROM recipes WHERE title LIKE ?', (pattern,)).fetchall())
# [('Breakfast burrito',)]
print(con.execute('SELECT title FROM recipes WHERE title = ?', (pattern,)).fetchall())
# []

from html import escape

note = '<b>More salsa</b> & more guacamole'
page = '<p>' + escape(note) + '</p>'
print(page)  # <p>&lt;b&gt;More salsa&lt;/b&gt; &amp; more guacamole</p>
print(note)  # <b>More salsa</b> & more guacamole

con.execute('UPDATE recipes SET note = ? WHERE id = ?', ('Fries & <salsa>', 1))
con.commit()
stored = con.execute('SELECT note FROM recipes WHERE id = 1').fetchone()[0]
print(stored)  # Fries & <salsa>
print(escape(stored))  # Tea &amp; &lt;cake&gt;

assert find_titles('California burrito') == ['California burrito']
assert find_titles('Quesadilla') == []
assert find_titles("Chef's burrito") == ["Chef's burrito"]
assert find_titles("California burrito' OR title='Breakfast burrito") == []
