您的位置:首页 > 移动开发 > Android开发

Android SQLite如何判断表是否存在

2016-04-02 23:32 591 查看
我使用如下代码完成

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”的方法,然后表不存会直接报错,因此不可以。上面的方法是最好的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: