删除SQLite中的现有表,当不支持IF EXISTS运算符时 [英] Drop existing table in SQLite, when IF EXISTS operator is not supported

查看:862
本文介绍了删除SQLite中的现有表,当不支持IF EXISTS运算符时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的SQLite版本不支持 IF EXISTS 运算符。如何删除可能存在或不存在的表,而不会在我处收到错误?

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?

我现在无法更新现有应用程序的版本,因此,我不能使用支持 IF EXISTS 的SQLite版本。

I can't update the version on a live application right now, so I cannot use a SQLite version that supports IF EXISTS.

推荐答案

官方文档说使用IF EXISTS,因此我怀疑您最好的计划是升级。

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.

这篇关于删除SQLite中的现有表,当不支持IF EXISTS运算符时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆