Last active
February 24, 2021 21:58
-
-
Save Pinacolada64/d2e97ef5e8c0d5b4696f6dbc2ec5cb81 to your computer and use it in GitHub Desktop.
testing for an author which does not exist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
def get_cursor(): | |
conn = sqlite3.connect("library.db") | |
return conn.cursor() | |
def select_all_records_by_author(cur, author): | |
sql = "SELECT * FROM books WHERE author=?" | |
cur.execute(sql, [author]) | |
""" | |
# adding check for no rows returned: | |
print(f"cur.rowcount = {cur.rowcount}") | |
if cur.rowcount < 1: | |
print(f"No books by author {author}.") | |
return | |
""" | |
print(cur.fetchall()) # or use fetchone() | |
print("\nHere is a listing of the rows in the table\n") | |
for row in cur.execute("SELECT rowid, * FROM books ORDER BY author"): | |
print(row) | |
def select_using_like(cur, text): | |
print("\nLIKE query results:\n") | |
sql = f""" | |
SELECT * FROM books | |
WHERE title LIKE '{text}%'""" | |
cur.execute(sql) | |
print(cur.fetchall()) | |
if __name__ == '__main__': | |
cursor = get_cursor() | |
select_all_records_by_author(cursor, author='Mike Driscoll') | |
# testing for an author which does not exist: | |
select_all_records_by_author(cursor, author='Ryan Sherwood') | |
select_using_like(cursor, text='Python') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rev 2 output:
I'm not in the database so it shouldn't list Mike's books!