0

This question asks for how it is possible to check for the existence of a database with sqlalchemy. I want to make my program a bit more "robust". If I provide a path to some file, there are several things that might go wrong. Let's assume the file does indeed exist.
First, it might not actually be a database file but for example a jpeg. I use the following code to connect to the DB:

import sqlalchemy

sqlalchemy.create_engine("sqlite:///database.db")

where the file pointed to is not a database. The above code example does not raise any Exceptions. How can I add a check for whether or not the file is a well-formed database?

Second, I want to read data from the database files associated with my application/script. However, if I provide some path to any valid sqlite database, sqlalchemy will happily read the data from this DB and create errors if the expected tables do not exist. But this might happen long after the connection to the database or not at all if by chance the tables I am trying to read exist in the database with just the right fields. How can I add a second check that all expected tables and only those exist in the DB? I am using ORM to create the data structures in python.

I summary, what is the best way to check for malformed database files with sqlalchemy? Of course, these checks are never 100% safe but they provide at least some level of safety. For example, they prevent the script from overwriting other files with databases.

HerpDerpington
  • 3,100
  • 4
  • 24
  • 39

0 Answers0