Sqlite判断表、字段是否存在

发布时间:2020-01-22编辑:佚名阅读(1315)

sqlite会自动维护一个系统表sqlite_master,该表存储了我们所创建的各个table, view, trigger等等信息。

sqlite_master表数据字段:

type:  类型,取值一般为table, view

name:    

tbl_name:   表名

rootpage:

sql:创建表或者视图的sql语句,可以从该sql语句中判断某字段是否存在

sqlite_master表结构如下:

CREATE TABLE sqlite_master (   
type TEXT,   
name TEXT,   
tbl_name TEXT,   
rootpage INTEGER,   
sql TEXT   
);

例如:

select * from sqlite_master where type = 'table' and name = 't_cmpt_cp'

1.   查询与判断表

查询sqlite中所有表,可用如下sql语句。

select name from sqlite_master where type='table' order by name;

我们可以通过如下语句查看这个内建表的所有记录:

select * from sqlite_master

由此可以进一步引申:判断指定的表是否存在,可以用如下语句:

select count(*)  from sqlite_master where type='table' and name = 'yourtablename';

或者

其中yourtablename表示你要判断的表名,如果查询结果大于0,表示该表存在于数据库中,否则不存在。

2.   查询与判断列

通过以下语句可查询出某个表的所有字段信息

PRAGMA  table_info([tablename])

比如:我想查看表catalog的所有列信息,可以用下述代码,结果如图所示:

PRAGMA  table_info(t_cmpt_cp)

判断某列是否存在的方法:

方法1:

/**
* 检查表中某列是否存在
* @param db
* @param tableName 表名
* @param columnName 列名
* @return
*/
private boolean checkColumnExists2(SQLiteDatabase db, String tableName
       , String columnName) {
    boolean result = false ;
    Cursor cursor = null ;
 
    try{
        cursor = db.rawQuery( "select * from sqlite_master where name = ? and sql like ?"
           , new String[]{tableName , "%" + columnName + "%"} );
        result = null != cursor && cursor.moveToFirst() ;
    }catch (Exception e){
        Log.e(TAG,"checkColumnExists2..." + e.getMessage()) ;
    }finally{
        if(null != cursor && !cursor.isClosed()){
            cursor.close() ;
        }
    }
 
    return result ;
}

方法2:

/** 
 * 判断某表里某字段是否存在 
 *  
 * @param db 
 * @param tableName 
 * @param fieldName 
 * @return 
 */  
private boolean isFieldExist(SQLiteDatabase db, String tableName, String fieldName) {  
    String queryStr = "select sql from sqlite_master where type = 'table' and name = '%s'";  
    queryStr = String.format(queryStr, tableName);  
    Cursor c = db.rawQuery(queryStr, null);  
    String tableCreateSql = null;  
    try {  
        if (c != null && c.moveToFirst()) {  
            tableCreateSql = c.getString(c.getColumnIndex("sql"));  
        }  
    } finally {  
        if (c != null)  
            c.close();  
    }  
    if (tableCreateSql != null && tableCreateSql.contains(fieldName))  
        return true;  
    return false;  
}

方法3:

根据 cursor.getColumnIndex(String columnName) 的返回值判断,如果为-1表示表中无此字段

/**
* 检查某表列是否存在
* @param db
* @param tableName 表名
* @param columnName 列名
* @return
*/
private boolean checkColumnExist1(SQLiteDatabase db, String tableName
        , String columnName) {
    boolean result = false ;
    Cursor cursor = null ;
    try{
        //查询一行
        cursor = db.rawQuery( "SELECT * FROM " + tableName + " LIMIT 0"
            , null );
        result = cursor != null && cursor.getColumnIndex(columnName) != -1 ;
    }catch (Exception e){
         Log.e(TAG,"checkColumnExists1..." + e.getMessage()) ;
    }finally{
        if(null != cursor && !cursor.isClosed()){
            cursor.close() ;
        }
    }
 
    return result ;
}

3.   Sqlite中新增、删除、重命名列

3.1   新增一列

方法:使用sql命令

命令:ALTER  TABLE   table-name ADD COLUMN  column-name column-type

例如:在student表中添加一列名为name,类型为varchar:

alter table student add column name varchar;
alter table catalog add column xxx1 char(20) default '';

3.2   删除一列

方法:由于drop命令在sqlite中不可用于删除列,

alter table student drop column name // 该行在SQlite中不能用,SQlite不支持drop

可采用如下思路,类似于swap()函数的过程。

比如我有表A,A中有x、y、z三列。我要将表A中的x列删掉。那么,

第1步,新建一个表B,B中含有y、z两个字段,且类型与A中的y、z类型相同。

第2步,将A中的所有y、z两列的值拷贝到B中。

上面两步使用一句命令即可完成

create table B as select y,z from A

注意,如果A中y的类型为char,则上面create命令会在B中创建类型为TEXT的y列。即char类型会被改变。

第3步,将A表删除

drop table if exists A

第4步,将B重命名为A

alter table B rename to A

3.3   重命名一列

方法:与删除一列相同,在sqlite中alter同样无法重命名一列。如果想重命名,那么思路与删除一列相同。

4.   Sqlite中新增、删除、重命名表

Sql语句在3.2中已有。整理如下。

4.1   新增表

create table A(id char(20),channeltext,name text,primary key (id))
create table B as select y,z from A

4.2   删除表

drop table if exists A

4.3   重命名表

alter table B rename to A


  关键字:Sqlite判断字段是否存在


鼓掌

0

正能量

0

1

呵呵

0


评论区