Skip to content

Instantly share code, notes, and snippets.

@Pinacolada64
Last active February 24, 2021 21:58
Show Gist options
  • Save Pinacolada64/d2e97ef5e8c0d5b4696f6dbc2ec5cb81 to your computer and use it in GitHub Desktop.
Save Pinacolada64/d2e97ef5e8c0d5b4696f6dbc2ec5cb81 to your computer and use it in GitHub Desktop.
testing for an author which does not exist
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')
@Pinacolada64
Copy link
Author

Output:

C:\Users\ryan-\PycharmProjects\TADA\venv\Scripts\python.exe C:/Users/ryan-/PycharmProjects/TADA/queries_original.py
[('Python 101', 'Mike Driscoll', '09/01/2020', 'Mouse vs. Python', 'epub'), ('Python Interviews', 'Mike Driscoll', '02/01/2018', 'Packt Publishing', 'softcover')]

Here is a listing of the rows in the table

(3, 'Automate the Boring Stuff with Python', 'Al Sweigart', '', 'No Starch Press', 'PDF')
(4, 'The Well-Grounded Python Developer', 'Doug Farrell', '2020', 'Manning', 'Kindle')
(1, 'Python 101', 'Mike Driscoll', '09/01/2020', 'Mouse vs. Python', 'epub')
(2, 'Python Interviews', 'Mike Driscoll', '02/01/2018', 'Packt Publishing', 'softcover')

LIKE query results:

[('Python 101', 'Mike Driscoll', '09/01/2020', 'Mouse vs. Python', 'epub'), ('Python Interviews', 'Mike Driscoll', '02/01/2018', 'Packt Publishing', 'softcover')]

Process finished with exit code 0

@Pinacolada64
Copy link
Author

Rev 2 output:

I'm not in the database so it shouldn't list Mike's books!

C:\Users\ryan-\PycharmProjects\TADA\venv\Scripts\python.exe C:/Users/ryan-/PycharmProjects/TADA/queries_original.py
[('Python 101', 'Mike Driscoll', '09/01/2020', 'Mouse vs. Python', 'epub'), ('Python Interviews', 'Mike Driscoll', '02/01/2018', 'Packt Publishing', 'softcover')]

Here is a listing of the rows in the table

(3, 'Automate the Boring Stuff with Python', 'Al Sweigart', '', 'No Starch Press', 'PDF')
(4, 'The Well-Grounded Python Developer', 'Doug Farrell', '2020', 'Manning', 'Kindle')
(1, 'Python 101', 'Mike Driscoll', '09/01/2020', 'Mouse vs. Python', 'epub')
(2, 'Python Interviews', 'Mike Driscoll', '02/01/2018', 'Packt Publishing', 'softcover')
[]

Here is a listing of the rows in the table

(3, 'Automate the Boring Stuff with Python', 'Al Sweigart', '', 'No Starch Press', 'PDF')
(4, 'The Well-Grounded Python Developer', 'Doug Farrell', '2020', 'Manning', 'Kindle')
(1, 'Python 101', 'Mike Driscoll', '09/01/2020', 'Mouse vs. Python', 'epub')
(2, 'Python Interviews', 'Mike Driscoll', '02/01/2018', 'Packt Publishing', 'softcover')

LIKE query results:

[('Python 101', 'Mike Driscoll', '09/01/2020', 'Mouse vs. Python', 'epub'), ('Python Interviews', 'Mike Driscoll', '02/01/2018', 'Packt Publishing', 'softcover')]

Process finished with exit code 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment