public class DatabaseUtils extends Object
限定符和类型 | 方法和说明 |
---|---|
static void |
addColumn(SQLiteDatabase db,
Class<?> clazz,
String... columns)
表添加列
|
static void |
createIndex(SQLiteDatabase db,
Class<?> clazz,
String indexName,
String... fieldNames)
创建表索引
|
static void |
createTable(SQLiteDatabase db,
Class<?> clazz)
创建表
|
static <T> T |
cursorToClassObject(Class<T> clazz,
Cursor cursor,
String[] columns)
查询记录的值,赋值给clazz的实例obj
|
static <T> T |
cursorToObject(T obj,
Cursor cursor,
String[] columns)
查询记录的值,赋值给obj
|
static void |
dropTable(SQLiteDatabase db,
Class<?> clazz)
删除表
|
static void |
dropTable(SQLiteDatabase db,
String table)
删除表
|
static Map<String,Field> |
getColumnNames(Class<?> clazz)
获取所有要数据库化的列名
|
static ContentValues |
getContentValues(Object object)
获取键值对象
|
static ContentValues |
getContentValues(Object object,
String[] columns)
获取键值对象
|
static List<Field> |
getDBFields(Class clazz)
获取所有有效的db字段 包含父类的
|
static String |
getDbType(Class<?> clazz)
获取sqlite对应的数据类型
|
static String |
getIdColumnName(Class<?> clazz) |
static Object |
getIdValue(Object object)
获取主键的值
|
static <T> void |
setFieldValue(T t,
Field field,
String columnName,
Cursor cursor)
field 赋值
|
public static void createIndex(SQLiteDatabase db, Class<?> clazz, String indexName, String... fieldNames)
db
- clazz
- public static void createTable(SQLiteDatabase db, Class<?> clazz)
db
- clazz
- public static void dropTable(SQLiteDatabase db, Class<?> clazz)
db
- clazz
- public static void dropTable(SQLiteDatabase db, String table)
db
- table
- public static void addColumn(SQLiteDatabase db, Class<?> clazz, String... columns)
db
- clazz
- public static Map<String,Field> getColumnNames(Class<?> clazz)
clazz
- public static Object getIdValue(Object object) throws IllegalAccessException
object
- IllegalAccessException
public static ContentValues getContentValues(Object object) throws IllegalAccessException
object
- IllegalAccessException
public static ContentValues getContentValues(Object object, String[] columns) throws IllegalAccessException
object
- columns
- IllegalAccessException
IllegalArgumentException
public static <T> T cursorToObject(T obj, Cursor cursor, String[] columns)
T
- obj
- cursor
- public static <T> T cursorToClassObject(Class<T> clazz, Cursor cursor, String[] columns) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException
T
- clazz
- cursor
- InstantiationException
IllegalAccessException
NoSuchMethodException
InvocationTargetException
public static <T> void setFieldValue(T t, Field field, String columnName, Cursor cursor)
T
- t
- field
- columnName
- cursor
-