日期:2014-05-16  浏览次数:20701 次

android数据库操作(一)

为了以后的项目使用Android数据库更方便,自已写了一个小的应用框架,不知道算不算称得上框架,反正自已用起来觉得还是挺方便的,参考Hibernate了应用思想。

?

业务介绍

1、建一个数据库madfiger.db

2、建一个用户表UserInfo,包含一些用户基本字段

?

好了那么我们现在来实现吧

?

1、新建UserInfo实体类,继承AbstractBaseModel,UserInfo的类名为数据库表名,字段分别为实现了Get与Set方式的实性字段;

?

UserInfo代码如下:

package cn.company.android.project.model;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import cn.madfinger.android.core.AbstractBaseModel;
import cn.madfinger.android.core.util.DateUtils;

public class UserInfo extends AbstractBaseModel{
	private static final long serialVersionUID = -7079062473198239915L;
	private String id;
	private String username;
	private String password;
	private String birthday;
	private int gender;
	private float weight;
	private Date createTime=DateUtils.long2Date(Calendar.getInstance(Locale.CHINESE).getTimeInMillis());
	public UserInfo(){
		super();
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getBirthday() {
		return birthday;
	}
	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
	public int getGender() {
		return gender;
	}
	public void setGender(int gender) {
		this.gender = gender;
	}
	public float getWeight() {
		return weight;
	}
	public void setWeight(float weight) {
		this.weight = weight;
	}
	public Date getCreateTime() {
		return createTime;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
}

?

?

AbstractBaseModel代码如下:

package cn.madfinger.android.core;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.json.JSONObject;

import android.util.Log;

/**
 * 实体模型基类
 * 
 * @author wiley
 */
public abstract class AbstractBaseModel implements Serializable {

	private static final long serialVersionUID = -4685989463430616599L;
	private static String TAG = AbstractBaseModel.class.getSimpleName();
	private static Map<String, String> TYPES = new HashMap<String, String>();
	
	static {
		TYPES.put("date", "TEXT");
		TYPES.put("string", "TEXT");
		TYPES.put("integer", "INTEGER");
		TYPES.put("int", "INTEGER");
		TYPES.put("short", "INTEGER");
		TYPES.put("long", "INTEGER");
		TYPES.put("float", "REAL");
		TYPES.put("double", "REAL");
	}

	public AbstractBaseModel() {
		TAG = this.getClass().getSimpleName();
	}

	public abstract String getId();
	public abstract void setId(String id);
	
	public Set<String> toFieldSet(){
		return this.toFieldMap().keySet();
	}

	public Map<String, String> toFieldMap() {
		Map<String, String> fieldMap = new HashMap<String, String>();
		Method[] methods = this.getClass().getMethods();
		try {
			String propertyName;
			String typeString;
			for (Method method : methods) {
				String methodName = method.getName();
				if (!methodName.startsWith("get")
						|| methodName.equalsIgnoreCase("getClass")
						|| methodName.equalsIgnoreCase("get"))
					continue;
				typeString = method.getReturnType().getSimpleName();
				propertyName = methodName.substring(3);
				fieldMap.put(propertyName, typeString);
			}
		} catch (Exception e) {
			Log.e(TAG, e.getMessage());
		}
		return fieldMap;
	}

	public String toCreateTableString() {
		StringBuilder sb = new StringBuilder();
		sb.append("CREATE TABLE ");
		sb.append(this.getClass().getSimpleName());
		sb.append(" (");
		int i = 0;
		Map<String, String> fieldMap = toFieldMap();
		Iterator<String> it = fieldMap.keySet().iterator();
		while