##############################################################################
# Python From Scratch
# Autor: Nilo Ney Coutinho Menezes
# Editora Novatec (c) 2010-2024
# Site: https://pythonfromscratch.com
#
# File: listing\chapter 11\11.05 - Query using parameters.py
# Description: Query using parameters
##############################################################################
import sqlite3
from contextlib import closing
name = input("Name to select: ")
with sqlite3.connect("phonebook.db") as connection:
with closing(connection.cursor()) as cursor:
cursor.execute('select * from phonebook where name = ?', (name,))
x = 0
while True:
result = cursor.fetchone()
if result is None:
if x == 0:
print("Nothing found.")
break
print(f"Name: {result[0]}\nPhone: {result[1]}")
x += 1