How do I check if the database file already exists or not? And, if the it exists, how do I check if it already has a specific table or not?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
To see if a database exists, you can
sqlite3.connect
to the file that you think contains the database, and try running a query on it. If it is not a database, you will get this error:sqlite3.connect
will create the database if it doesn't exist; as @johnp points out in the comments,os.path.exists
will tell you whether the file exists.To check for existing tables, you query against sqlite_master. For example: