Android Sqlite 与 dao
来源:互联网 发布:越南南海知乎 编辑:程序博客网 时间:2024/06/07 01:52
public static <T> List<T> readAllTrans(T t, DBManager dbHelper, String tableName) {
dbHelper.openDatabase();
SQLiteDatabase db = dbHelper.getDatabase();
Cursor cursor = db.query(tableName, null, null, null, null, null, null);
List<T> dataList = new ArrayList<T>();
//表中各项名称
List<String> columnNames = new ArrayList<String>(Arrays.asList(cursor.getColumnNames()));
columnNames.remove("id");
//对表中各项进行排序,反射得到的set方法是按升序排列,便于匹配
Collections.sort(columnNames);
System.out.println("columnNames2" + columnNames.size());
try {
Class<? extends Object> clazz = t.getClass();
Method[] declaredMethods = clazz.getDeclaredMethods();
List<Method> allSetMethods = new ArrayList<Method>();
List<Method> setMethods = new ArrayList<Method>();
//获取所有set方法
for(int i = 0; i < declaredMethods.length; i++){
String methodName = declaredMethods[i].getName();
if(methodName.startsWith("set") ){
allSetMethods.add(declaredMethods[i]);
}
}
//获取与数据库ColumnName相符的set方法
for(int i = 0; i < columnNames.size(); i++){
for(int j = i; j < allSetMethods.size(); j++){
String methodName = allSetMethods.get(j).getName();
if( columnNames.get(i).equalsIgnoreCase(methodName.substring(3, methodName.length()))){
setMethods.add(allSetMethods.get(j));
continue;
}
}
}
System.out.println("columnNames.size() : " + columnNames.size() + "setMethods.size() : " + setMethods.size());
if (cursor != null && cursor.moveToFirst()) {
do {
t = (T) clazz.newInstance();
for(int i = 0; i < setMethods.size(); i++ ){
Method method = setMethods.get(i);
Class<?>[] parameterTypes = method.getParameterTypes();
int columnIndex = cursor.getColumnIndex(columnNames.get(i));
if (parameterTypes[0] == String.class) {
method.invoke(t,cursor.getString(columnIndex));
}else if (parameterTypes[0] == int.class) {
method.invoke(t, cursor.getInt(columnIndex));
}else if (parameterTypes[0] == long.class) {
method.invoke(t, cursor.getLong(columnIndex));
}else if (parameterTypes[0] == byte.class) {
method.invoke(t, (byte)cursor.getInt(columnIndex));
}else if (parameterTypes[0] == boolean.class) {
method.invoke(t, cursor.getInt(columnIndex) == 0 ? false:true);
}else if(parameterTypes[0] == byte[].class){
method.invoke(t, cursor.getBlob(columnIndex));
}
}
dataList.add(t);
}while (cursor.moveToNext());
}
} catch (InstantiationException e ) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
dbHelper.closeDatabase();
return dataList;
}
dbHelper.openDatabase();
SQLiteDatabase db = dbHelper.getDatabase();
Cursor cursor = db.query(tableName, null, null, null, null, null, null);
List<T> dataList = new ArrayList<T>();
//表中各项名称
List<String> columnNames = new ArrayList<String>(Arrays.asList(cursor.getColumnNames()));
columnNames.remove("id");
//对表中各项进行排序,反射得到的set方法是按升序排列,便于匹配
Collections.sort(columnNames);
System.out.println("columnNames2" + columnNames.size());
try {
Class<? extends Object> clazz = t.getClass();
Method[] declaredMethods = clazz.getDeclaredMethods();
List<Method> allSetMethods = new ArrayList<Method>();
List<Method> setMethods = new ArrayList<Method>();
//获取所有set方法
for(int i = 0; i < declaredMethods.length; i++){
String methodName = declaredMethods[i].getName();
if(methodName.startsWith("set") ){
allSetMethods.add(declaredMethods[i]);
}
}
//获取与数据库ColumnName相符的set方法
for(int i = 0; i < columnNames.size(); i++){
for(int j = i; j < allSetMethods.size(); j++){
String methodName = allSetMethods.get(j).getName();
if( columnNames.get(i).equalsIgnoreCase(methodName.substring(3, methodName.length()))){
setMethods.add(allSetMethods.get(j));
continue;
}
}
}
System.out.println("columnNames.size() : " + columnNames.size() + "setMethods.size() : " + setMethods.size());
if (cursor != null && cursor.moveToFirst()) {
do {
t = (T) clazz.newInstance();
for(int i = 0; i < setMethods.size(); i++ ){
Method method = setMethods.get(i);
Class<?>[] parameterTypes = method.getParameterTypes();
int columnIndex = cursor.getColumnIndex(columnNames.get(i));
if (parameterTypes[0] == String.class) {
method.invoke(t,cursor.getString(columnIndex));
}else if (parameterTypes[0] == int.class) {
method.invoke(t, cursor.getInt(columnIndex));
}else if (parameterTypes[0] == long.class) {
method.invoke(t, cursor.getLong(columnIndex));
}else if (parameterTypes[0] == byte.class) {
method.invoke(t, (byte)cursor.getInt(columnIndex));
}else if (parameterTypes[0] == boolean.class) {
method.invoke(t, cursor.getInt(columnIndex) == 0 ? false:true);
}else if(parameterTypes[0] == byte[].class){
method.invoke(t, cursor.getBlob(columnIndex));
}
}
dataList.add(t);
}while (cursor.moveToNext());
}
} catch (InstantiationException e ) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
dbHelper.closeDatabase();
return dataList;
}
0 0
- Android Sqlite 与 dao
- Android SQLite 数据库DAO 基本方法
- android 之sqlite数据库Dao三层模型
- android 与sqlite数据库
- Android 与 SQLite
- Android (SQLite 数据库与ContentProvider)
- Sqlite数据库操作Dao类
- Android DAO
- phonegap sqlite与android sqlite的存储差别
- android sqlite 的创建与使用,sqlite自定义框架。
- android 的sqlite 主键与simpleCursorAdapter
- android 的sqlite 主键与simpleCursorAdapter
- android 的sqlite 主键与simpleCursorAdapter
- android listview与sqlite数据绑定
- [Android SQLite]数据存储与访问 - SharedPreferences
- 轻型数据库SQLite与Android实例
- Android学习--SQLite与简单调试方法
- Android---SQLite的介绍与相关操作方法
- Mac下配置Groovy
- 深入浅出Mybatis系列(六)---objectFactory、plugins、mappers简介与配置
- 已发布文章相关资源下载地址
- Python3.0及以上 fatal error: Python.h: 错误解决
- angular 服务
- Android Sqlite 与 dao
- centos6.8系统radius认证服务器的搭建流程
- 表单序列化
- Android开发线程间的交互之EventBus简单使用
- iOS开发基础之谓词(NSPredicate)的使用
- POJ3255_Roadlocks_dijkstra求次短路
- 【PM】你的团队可能会出现可怕的“死海效应”
- Spring Boot 学习9--使用JdbcTemplate
- win10处理msi安装包安装出错2502、2503问题