Click here to Skip to main content
15,065,324 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My desktop application allows the user to create and open SQlite database files. The database has specific tables and queries that would likely throw exceptions when working with just any SQlite file.

How can I ensure that the user can only open certain database files without throwing exceptions?


What I have tried:

1. Appending to filename with a string such as "(App)".
2. Creating a table with just one "key" in it then looking for the existence of that key.

These seem 'clunky' and prone to problems.
Updated 5-Aug-21 6:50am
Richard MacCutchan 5-Aug-21 12:01pm
Just tell them the name of the file.

Alternatively, you could make your application user friendly by catching the exceptions and posting a message to the user that explains what they have done wrong.
Mark F. 5-Aug-21 12:42pm
The application allows a user to create a file and name it anything they wish.

What happens if the user tries to open an SQlite database from disk that does not contain the created tables?
Richard MacCutchan 5-Aug-21 12:51pm
It will most likely throw an exception. So, as I said, your code should catch any exceptions and provide a useful message to the user. Remember, just like the rest of us, users can make mistakes. An application should ensure it helps them to know what they did wrong.

Try a few tests to see what happens with different scenarios: file not found, file not in SQLite format, tables missing, table has no content, etc.
Mark F. 6-Aug-21 10:35am
Thanks for the help!
PIEBALDconsult 5-Aug-21 14:00pm
Have a quick health-check when the file is opened, report any issues.
Mark F. 6-Aug-21 10:35am

1 solution

The simplest solution is to open the file, and check what tables it contains:
SELECT name FROM sqlite_master WHERE type = 'table'

Should do it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900