1134

How do I, reliably, check in SQLite, whether a particular user table exists?

I am not asking for unreliable ways like checking if a "select *" on the table returned an error or not (is this even a good idea?).

The reason is like this:

In my program, I need to create and then populate some tables if they do not exist already.

If they do already exist, I need to update some tables.

Should I take some other path instead to signal that the tables in question have already been created - say for example, by creating/putting/setting a certain flag in my program initialization/settings file on disk or something?

Or does my approach make sense?

1
  • See Ravens comment SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type="table" AND name="table_name"); seems to really cut the result down to a boolean result (either 0 or 1, nothing else) Commented Sep 16, 2023 at 12:52

31 Answers 31

1
2
-3

Look into the "try - throw - catch" construct in C++. Most other programming languages have a similar construct for handling errors.

Sign up to request clarification or add additional context in comments.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.