美文网首页
SQLite更新表

SQLite更新表

作者: mapleSeriesX | 来源:发表于2018-12-25 13:39 被阅读0次
    /**
         * 判断某表里某字段是否存在
         *
         * @param db
         * @param tableName
         * @param fieldName
         * @return
         */
        public 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;
        }
     
    /**更新table_name表 增加field_name字段**/
                if (!dbHelper.isFieldExist(db, "table_name", "field_name")) {
                    db.execSQL("ALTER TABLE table_name ADD COLUMN field_name varchar(50)");
                }
    

    相关文章

      网友评论

          本文标题:SQLite更新表

          本文链接:https://www.haomeiwen.com/subject/tjhflqtx.html