Drop existing table in SQLite, when IF EXISTS operator is not supported

前端 未结 4 1692
余生分开走
余生分开走 2020-12-13 00:09

My version of SQLite does not support the IF EXISTS operator. How can I drop a table which may or may not exist without getting an error slapped at me?

4条回答
  •  盖世英雄少女心
    2020-12-13 00:48

    The official documentation says to use IF EXISTS, so I suspect your best plan is to upgrade.

    If you can't, you need to see whether you can do some trivial operation on the table that will succeed whether or not the table is empty; if it succeeds you should delete the table, if it fails the table is already gone. An example of the sort of operation to try might be:

    SELECT COUNT(*) FROM theTable;
    

    Note that you need to trap the possible error from this at the language level, and you might want to wrap the whole lot (probe, error-trap, drop table) in a transaction. Of course, the other approach if you're getting into error handling is to just drop the table and handle the error anyway.

提交回复
热议问题