sqlite数据处理工具

1,在定义变量名称时,使用实体属性和数据库列名一致,方便后续处理

2,工具类通过java反射技术编写

3,在使用数据库操作,查询时Cursor封装实体属性值,修改时实体对象转换为ContentValues

4,方法中提供两种方式转换,使用public 属性,private 属性 通过getset方法使值对象话

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;

import android.content.ContentValues;
import android.database.Cursor;
/**
 * 
 *@date 2015-7-8 
 *@author zhengshijun
 *
 */
public class SqliteUtils 
{
	public static <T> T cursorToEntity(Cursor cursor,Class<T> classEntity) throws InstantiationException, IllegalAccessException{
		T o = null;
		o = classEntity.newInstance();
		Class<?> entity = o.getClass();
		Field[] fields = entity.getFields();
		for(Field field:fields){
			if(Modifier.isStatic(field.getModifiers()))continue;
			String name = field.getName();
			int index = cursor.getColumnIndex(name);
			if(index==-1)continue;
			Class<?> type = field.getType();
			if(type == String.class){
				field.set(o, cursor.getString(index));
			}else if(type == Integer.class||type==int.class){
				field.setInt(o, cursor.getInt(index));
			}else if(type == Double.class||type == double.class ){
				field.setDouble(o, cursor.getDouble(index));
			}else if(type == Float.class|| type==float.class){
				field.setFloat(o, cursor.getFloat(index));
			}else if(type == Long.class||type == long.class){
			    field.setLong(o, cursor.getLong(index));
			}
		}
		
		return o;
		
	}
	public static ContentValues entityToValues(Serializable entity) throws IllegalArgumentException, IllegalAccessException{
		ContentValues values =null;
		if(entity!=null){
			values = new ContentValues();
			Class<?> classEntity = entity.getClass();
			Field[] fields =classEntity.getFields();
			for(Field field:fields){
				if(Modifier.isStatic(field.getModifiers()))continue;
				String name = field.getName();
				Class<?> type = field.getType();
				if(type == String.class){
					Object value = field.get(entity);
					if(value==null)continue;
					values.put(name, value.toString());
				}else if(type == Integer.class||type==int.class){
					Integer value = field.getInt(entity);
					if(value==null)continue;
					values.put(name, value);
				}else if(type == Double.class||type == double.class ){
					Double value = field.getDouble(entity);
					if(value==null)continue;
					values.put(name, value);
				}else if(type == Float.class|| type==float.class){
					Float value = field.getFloat(entity);
					if(value==null)continue;
					values.put(name, value);
				}else if(type == Long.class||type == long.class){
					Long value = field.getLong(entity);
					if(value==null)continue;
					values.put(name, value);
				}
				
			}
		}
		
		return values;
	}
	
	public static <T> T cursorToEntity2(Cursor cursor,Class<T> classEntity) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		T o = null;
		o = classEntity.newInstance();
		Class<?> entity = o.getClass();
		Method methods[] = entity.getMethods();
		for(Method method:methods){
			if(Modifier.isStatic(method.getModifiers()))continue;
			String methodName = method.getName();
			if(methodName.startsWith("set")){
				Type types[] = method.getGenericParameterTypes();
				if(method.getGenericReturnType().equals(void.class)&&types.length==1){
					String name = Character.toLowerCase(methodName.charAt(3))+methodName.substring(4);
					int index = cursor.getColumnIndex(name);
					if(index==-1)continue;
					Type type = types[0];
					if(type == String.class){
						method.invoke(o, cursor.getString(index));
					}else if(type == Integer.class||type==int.class){
						method.invoke(o, cursor.getInt(index));
					}else if(type == Double.class||type == double.class ){
						method.invoke(o, cursor.getDouble(index));
					}else if(type == Float.class|| type==float.class){
						method.invoke(o, cursor.getFloat(index));
					}else if(type == Long.class||type == long.class){
						method.invoke(o, cursor.getLong(index));
					}else if(type==java.util.Date.class){
						
					}else if(type ==java.sql.Date.class){
					 
					}
					
				}
			}
			
		}
		return o;
	}
	public static ContentValues entityToValues2(Serializable entity) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{
		ContentValues values =null;
		if(entity!=null){
			values = new ContentValues();
			Class<?> classEntity = entity.getClass();
			Method methods[] = classEntity.getMethods();
			for(Method method:methods){
				if(Modifier.isStatic(method.getModifiers()))continue;
				String methodName = method.getName();
				if(methodName.startsWith("get")){
					Type types[] = method.getGenericParameterTypes();
					Type type = method.getReturnType();
					if(types.length==0&&!type.equals(void.class)){
						String name = Character.toLowerCase(methodName.charAt(3))+methodName.substring(4);
						if(type == String.class){
							Object value =method.invoke(entity);
							if(value==null)continue;
							values.put(name, value.toString());
						}else if(type == Integer.class||type==int.class){
							Integer value = (Integer) method.invoke(entity);
							if(value==null)continue;
							values.put(name, value);
						}else if(type == Double.class||type == double.class ){
							Double value = (Double) method.invoke(entity);
							if(value==null)continue;
							values.put(name, value);
						}else if(type == Float.class|| type==float.class){
							Float value = (Float) method.invoke(entity);
							if(value==null)continue;
							values.put(name, value);
						}else if(type == Long.class||type == long.class){
							Long value = (Long) method.invoke(entity);
							if(value==null)continue;
							values.put(name, value);
						}
					}
				}
			}
		}
		return values;
		
	}
	
}

 

 

你可能感兴趣的:(sqlite)