设为首页 收藏本站
查看: 1260|回复: 0

[经验分享] sqlite数据处理工具

[复制链接]

尚未签到

发表于 2016-11-30 08:41:56 | 显示全部楼层 |阅读模式
  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;
}
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-307430-1-1.html 上篇帖子: sqlite 比较时间字符串 下篇帖子: Android平台SQLite快速入门实践
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表