我使用如下代码完成
public boolean tabbleIsExist(SQLiteDatabase db, String tableName){
boolean result = false;
if(tableName == null){
return false;
}
Cursor cursor = null;
try {
String sql = "select count(*) as c from Sqlite_master where type ='table' and name ='"+tableName.trim()+"' ";
cursor = db.rawQuery(sql, null);
if(cursor.moveToNext()){
int count = cursor.getInt(0);
if(count>0){
result = true;
}
}
} catch (Exception e) {
// TODO: handle exception
}
return result;
}
之前我采用“select * from table”的方法,然后表不存会直接报错,因此不可以。上面的方法是最好的
检查SQLite表存在性
本文介绍了一种通过查询SQLite_master表来判断指定表是否存在的方法,这种方法避免了因表不存在而导致程序异常的问题。
7514

被折叠的 条评论
为什么被折叠?



