8. RDBMS and SQL
Activity 8.01: Retrieving Data Accurately from Databases
Solution:
These are the steps to complete this activity:
- Connect to the supplied
petsdb
database:import sqlite3 conn = sqlite3.connect("petsdb")
- Write a function to check whether the connection has been successful:
# a tiny function to make sure the connection is successful def is_opened(conn): try: conn.execute("SELECT * FROM persons LIMIT 1") return True except sqlite3.ProgrammingError as e: print("Connection closed {}".format(e)) return False print(is_opened(conn))
The output is as follows:
True
- Close the connection:
conn.close()
- Check whether the connection is open or closed:
print(is_opened(conn))
The output is as follows...