我如何可靠地在SQLite中检查特定的用户表是否存在?
我并不是要求使用不可靠的方法,比如检查表上的“select *”是否返回错误(这是一个好主意吗?)
原因如下:
在我的程序中,我需要创建并填充一些表,如果它们还不存在的话。
如果它们已经存在,我需要更新一些表。
我是否应该采取其他路径来表示已经创建了相关的表-例如,通过在磁盘上的程序初始化/设置文件中创建/放置/设置某个标志?
或者我的方法有意义吗?
我如何可靠地在SQLite中检查特定的用户表是否存在?
我并不是要求使用不可靠的方法,比如检查表上的“select *”是否返回错误(这是一个好主意吗?)
原因如下:
在我的程序中,我需要创建并填充一些表,如果它们还不存在的话。
如果它们已经存在,我需要更新一些表。
我是否应该采取其他路径来表示已经创建了相关的表-例如,通过在磁盘上的程序初始化/设置文件中创建/放置/设置某个标志?
或者我的方法有意义吗?
当前回答
一种变体是使用SELECT COUNT(*)而不是SELECT NAME,即。
SELECT count(*) FROM sqlite_master WHERE type='table' AND name='table_name';
如果表不存在,返回0,如果存在,返回1。这可能在你的编程中很有用,因为数值结果处理起来更快/更容易。下面说明了如何在Android中使用SQLiteDatabase, Cursor, rawQuery带参数来做到这一点。
boolean tableExists(SQLiteDatabase db, String tableName)
{
if (tableName == null || db == null || !db.isOpen())
{
return false;
}
Cursor cursor = db.rawQuery(
"SELECT COUNT(*) FROM sqlite_master WHERE type = ? AND name = ?",
new String[] {"table", tableName}
);
if (!cursor.moveToFirst())
{
cursor.close();
return false;
}
int count = cursor.getInt(0);
cursor.close();
return count > 0;
}
其他回答
这是我使用的函数:
给定SQLDatabase Object = db
public boolean exists(String table) {
try {
db.query("SELECT * FROM " + table);
return true;
} catch (SQLException e) {
return false;
}
}
R DBI包中的函数dbExistsTable()为R程序员简化了这个问题。请看下面的例子:
library(DBI)
con <- dbConnect(RSQLite::SQLite(), ":memory:")
# let us check if table iris exists in the database
dbExistsTable(con, "iris")
### returns FALSE
# now let us create the table iris below,
dbCreateTable(con, "iris", iris)
# Again let us check if the table iris exists in the database,
dbExistsTable(con, "iris")
### returns TRUE
如果你使用SQLite 3.3+版本,你可以很容易地创建一个表:
create table if not exists TableName (col1 typ1, ..., colN typN)
以同样的方式,你可以删除一个表,如果它存在,使用:
drop table if exists TableName
如果你正在使用fmdb,我认为你可以导入FMDatabaseAdditions并使用bool函数:
[yourfmdbDatabase tableExists:tableName].
如果你得到一个"table already exists"错误,在SQL字符串中做如下修改:
CREATE table IF NOT EXISTS table_name (para1,para2);
这样可以避免异常。