对于sqlite的使用仅仅封装成一个类,目的就是为了那些对于sqlite使用不太熟悉的人准备的,有此代码在手,再也不怕玩不转sqlite了。话不多说直接看代码:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
/**
* 数据库工具类
*
* @Description: TODO(这里用一句话描述这个类的作用)
* @date 2014-10-28 上午10:46:06
* @UpdateData 2014-10-28 上午10:46:06 by_
*/
public class SQLiteUtils extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "xxxxx_db.db";
private static final int DATABASE_VERSION = 1;
private static final String KEY_ROWID = "_id";
private static SQLiteUtils instance;
private Cursor cursor = null;
private static File path = new File("/ooooo/" + DATABASE_NAME);
private SQLiteUtils(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public static SQLiteUtils getInstance(Context context) {
if (instance == null) {
instance = new SQLiteUtils(context);
}
return instance;
}
/**
* TODO 关闭数据库
*
* @param mDb
*/
public void Close(SQLiteDatabase mDb) {
if (mDb.isOpen() && !mDb.isDbLockedByOtherThreads()
&& !mDb.isDbLockedByCurrentThread()) {
if (cursor != null) {
if (!cursor.isClosed()) {
try {
cursor.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null)
cursor.close();
cursor = null;
}
} else {
cursor = null;
}
}
mDb.close();
} else {
}
}
/**
* TODO 存储数据,参数放一个list即可
*
* @param mDb
* @param list
*/
public long SavaData(SQLiteDatabase mDb, List list) {
if (list == null || list.size() == 0) {
return -1;
}
Class clazz = list.get(0).getClass();
checkTableExist(mDb, clazz);
String table = clazz.getSimpleName();
Field[] fields = clazz.getDeclaredFields();
long row = -1;
try {
for (int i = 0; i < list.size(); i++) {
ContentValues initialValues = new ContentValues();
Object o = list.get(i);
for (int j = 0; j < fields.length; j++) {
fields[j].setAccessible(true);
Class<?> dataType = fields[j].getType();
Object value = fields[j].get(o);
if (value != null) {
if (!KEY_ROWID.equals(fields[j].getName())) {
if (dataType == String.class) {
initialValues.put(fields[j].getName(),
String.valueOf(value));
} else if (dataType == int.class
|| dataType == Integer.class) {
initialValues.put(
fields[j].getName(),
value == null ? (Integer) null
: Integer.parseInt(value
.toString()));
} else if (dataType == float.class
|| dataType == Float.class) {
initialValues.put(
fields[j].getName(),
value == null ? (Float) null : Float
.parseFloat(value.toString()));
} else if (dataType == double.class
|| dataType == Double.class) {
initialValues.put(
fields[j].getName(),
value == null ? (Double) null : Double
.parseDouble(value.toString()));
} else if (dataType == long.class
|| dataType == Long.class) {
initialValues.put(
fields[j].getName(),
value == null ? (Long) null : Long
.parseLong(value.toString()));
} else if (dataType == boolean.class
|| dataType == Boolean.class) {
initialValues.put(
fields[j].getName(),
value == null ? (Boolean) null : "1"
.equals(value.toString()));
} else {
initialValues.put(fields[j].getName(),
String.valueOf(value));
}
}
}
}
row = mDb.insert(table, null, initialValues);
}
return row;
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return -1;
}
/**
* TODO 单条数据插入,传入一个Bean对象
*
* @param mDb
* @param o
* @return
*/
public long SavaDataSingle(SQLiteDatabase mDb, Object o) {
if (o == null) {
return -1;
}
checkTableExist(mDb, o.getClass());
String table = o.getClass().getSimpleName();
Field[] fields = o.getClass().getDeclaredFields();
try {
ContentValues initialValues = new ContentValues();
for (int j = 0; j < fields.length; j++) {
fields[j].setAccessible(true);
Class<?> dataType = fields[j].getType();
Object value = fields[j].get(o);
if (value != null) {
if (!KEY_ROWID.equals(fields[j].getName())) {
if (dataType == String.class) {
initialValues.put(fields[j].getName(),
String.valueOf(value));
} else if (dataType == int.class
|| dataType == Integer.class) {
initialValues.put(
fields[j].getName(),
value == null ? (Integer) null : Integer
.parseInt(value.toString()));
} else if (dataType == float.class
|| dataType == Float.class) {
initialValues.put(
fields[j].getName(),
value == null ? (Float) null : Float
.parseFloat(value.toString()));
} else if (dataType == double.class
|| dataType == Double.class) {
initialValues.put(
fields[j].getName(),
value == null ? (Double) null : Double
.parseDouble(value.toString()));
} else if (dataType == long.class
|| dataType == Long.class) {
initialValues.put(
fields[j].getName(),
value == null ? (Long) null : Long
.parseLong(value.toString()));
} else if (dataType == boolean.class
|| dataType == Boolean.class) {
initialValues.put(
fields[j].getName(),
value == null ? (Boolean) null : "1"
.equals(value.toString()));
} else {
initialValues.put(fields[j].getName(),
String.valueOf(value));
}
}
}
}
long _id = mDb.insert(table, null, initialValues);
return _id;
} catch (Exception e) {
e.printStackTrace();
}
return -1;
}
/**
* TODO 检查是否已经存在table
*
* @param mDb
* @param clazz
*/
private void checkTableExist(SQLiteDatabase mDb, Class<?> clazz) {
if (!tableIsExist(mDb, clazz)) {
Log.d("GGG", "从新建表");
CreateTable(mDb, clazz);
}
}
/**
* TODO 通过语句检验是否已经存在table
*
* @param mDb
* @param clazz
* @return
*/
public boolean tableIsExist(SQLiteDatabase mDb, Class<?> clazz) {
if (cursor != null) {
cursor = null;
}
try {
String sql = "SELECT COUNT(*) AS c FROM sqlite_master WHERE type ='table' AND name ='"
+ clazz.getSimpleName() + "' ";
cursor = mDb.rawQuery(sql, null);
if (cursor != null && cursor.moveToNext()) {
int count = cursor.getInt(0);
if (count > 0) {
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null)
cursor.close();
cursor = null;
}
return false;
}
/**
* TODO 删除table中的所有数据
*
* @param mDb
* @param clazz
*/
public void DeleteData(SQLiteDatabase mDb, Class clazz) {
checkTableExist(mDb, clazz);
try {
String table = clazz.getSimpleName();
mDb.delete(table, null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
public Object GetAllData(SQLiteDatabase mDb, Class clazz) {
return GetAllData(mDb, clazz, null);
}
/**
* TODO 获取所有数据
*
* @param mDb
* @param clazz
* @return
*/
public Object GetAllData(SQLiteDatabase mDb, Class clazz, String orderBy) {
if (cursor != null) {
cursor = null;
}
checkTableExist(mDb, clazz);
List obList = new ArrayList();
Field[] fields = clazz.getDeclaredFields();
String[] names = new String[fields.length];
for (int i = 0; i < fields.length; i++) {
names[i] = fields[i].getName();
}
cursor = mDb.query(clazz.getSimpleName(), null, null, null, null, null,
orderBy);
if (cursor != null) {
try {
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
.moveToNext()) {
Object obj = clazz.newInstance();
for (Field f : fields) {
f.setAccessible(true);
// /用反射来调用相应的方法
// /先构造出方法的名字
String CursorGettypeName = f.getType().getSimpleName();
if ("Integer".equals(CursorGettypeName)) {
CursorGettypeName = "int";
} else if ("boolean".equals(CursorGettypeName)) {
CursorGettypeName = "String";
}
CursorGettypeName = CursorGettypeName.substring(0, 1)
.toUpperCase() + CursorGettypeName.substring(1);
String CursorGetName = "get" + CursorGettypeName;
String BeanSetType = f.getName();
String BeanSetName = "set"
+ BeanSetType.substring(0, 1).toUpperCase()
+ BeanSetType.substring(1);
Method BeanSet;
if (f.getType() == boolean.class) {
BeanSet = getBooleanFieldSetMethod(clazz, f);
} else {
BeanSet = clazz.getMethod(BeanSetName, f.getType());
}
Object retValue = cursor.getString(cursor
.getColumnIndex(f.getName()));
Class<?> dataType = f.getType();
if (retValue != null) {
if (dataType == String.class) {
if (retValue != null) {
BeanSet.invoke(obj, retValue.toString());
} else {
BeanSet.invoke(obj, "");
}
} else if (dataType == int.class
|| dataType == Integer.class) {
BeanSet.invoke(
obj,
retValue == null ? (Integer) null
: Integer.parseInt(retValue
.toString()));
} else if (dataType == float.class
|| dataType == Float.class) {
BeanSet.invoke(
obj,
retValue == null ? (Float) null
: Float.parseFloat(retValue
.toString()));
} else if (dataType == double.class
|| dataType == Double.class) {
BeanSet.invoke(
obj,
retValue == null ? (Double) null
: Double.parseDouble(retValue
.toString()));
} else if (dataType == long.class
|| dataType == Long.class) {
BeanSet.invoke(
obj,
retValue == null ? (Long) null : Long
.parseLong(retValue.toString()));
} else if (dataType == boolean.class
|| dataType == Boolean.class) {
BeanSet.invoke(
obj,
retValue == null ? (Boolean) null : "1"
.equals(retValue.toString()));
} else {
BeanSet.invoke(obj, retValue);
}
}
}
obList.add(obj);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null)
cursor.close();
cursor = null;
}
return obList;
} else {
return null;
}
}
/**
* TODO 分页查询
*
* @param mDb
* @param clazz
* @param num
* 每页查询多少个
* @param currentPage
* 每次查询的页码
* @param where
* 模糊查询语句
* @return
*/
public Object GetDataByPage(SQLiteDatabase mDb, Class clazz, int num,
int currentPage, String where) {
if (cursor != null) {
cursor = null;
}
checkTableExist(mDb, clazz);
List obList = new ArrayList();
Field[] fields = clazz.getDeclaredFields();
String[] names = new String[fields.length];
for (int i = 0; i < fields.length; i++) {
names[i] = fields[i].getName();
}
// cursor = mDb.rawQuery("select * from " + table + " limit " + num
// + " offset " + (num * (currentPage - 1)), null);
cursor = mDb.rawQuery("select * from " + clazz.getSimpleName()
+ " where " + where + " limit " + num + " offset "
+ (num * (currentPage - 1)), null);
if (cursor != null) {
try {
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
.moveToNext()) {
Object obj = clazz.newInstance();
for (Field f : fields) {
f.setAccessible(true);
// /用反射来调用相应的方法
// /先构造出方法的名字
String CursorGettypeName = f.getType().getSimpleName();
if ("Integer".equals(CursorGettypeName)) {
CursorGettypeName = "int";
} else if ("boolean".equals(CursorGettypeName)) {
CursorGettypeName = "String";
}
CursorGettypeName = CursorGettypeName.substring(0, 1)
.toUpperCase() + CursorGettypeName.substring(1);
String CursorGetName = "get" + CursorGettypeName;
String BeanSetType = f.getName();
String BeanSetName = "set"
+ BeanSetType.substring(0, 1).toUpperCase()
+ BeanSetType.substring(1);
Method BeanSet;
if (f.getType() == boolean.class) {
BeanSet = getBooleanFieldSetMethod(clazz, f);
} else {
BeanSet = clazz.getMethod(BeanSetName, f.getType());
}
Object retValue = cursor.getString(cursor
.getColumnIndex(f.getName()));
Class<?> dataType = f.getType();
if (retValue != null) {
if (dataType == String.class) {
if (retValue != null) {
BeanSet.invoke(obj, retValue.toString());
} else {
BeanSet.invoke(obj, "");
}
} else if (dataType == int.class
|| dataType == Integer.class) {
BeanSet.invoke(
obj,
retValue == null ? (Integer) null
: Integer.parseInt(retValue
.toString()));
} else if (dataType == float.class
|| dataType == Float.class) {
BeanSet.invoke(
obj,
retValue == null ? (Float) null
: Float.parseFloat(retValue
.toString()));
} else if (dataType == double.class
|| dataType == Double.class) {
BeanSet.invoke(
obj,
retValue == null ? (Double) null
: Double.parseDouble(retValue
.toString()));
} else if (dataType == long.class
|| dataType == Long.class) {
BeanSet.invoke(
obj,
retValue == null ? (Long) null : Long
.parseLong(retValue.toString()));
} else if (dataType == boolean.class
|| dataType == Boolean.class) {
BeanSet.invoke(
obj,
retValue == null ? (Boolean) null : "1"
.equals(retValue.toString()));
} else {
BeanSet.invoke(obj, retValue);
}
}
}
obList.add(obj);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null)
cursor.close();
cursor = null;
}
return obList;
} else {
return null;
}
}
/**
* TODO 根据主键删除
*
* @param mDb
* @param clazz
* @param id
*/
public void DeleteById(SQLiteDatabase mDb, Class clazz, String id) {
try {
String tableName = clazz.getSimpleName();
mDb.delete(tableName, KEY_ROWID + " = " + id, null);
} catch (Exception e) {
}
}
/**
* TODO 根据SQL语句中where删除
*
* @param mDb
* @param clazz
* @param where
* 方法事例 :DeleteByWhere(db,Bean.class,"spmc like '五角枫'")
*/
public void DeleteByWhere(SQLiteDatabase mDb, Class clazz, String where) {
try {
// Demo SQL mDb.delete(tableName, "spmc like '五角枫'", null);
mDb.delete(clazz.getSimpleName(), where, null);
} catch (Exception e) {
}
}
/**
* TODO 根据编写SQL语句模糊查询
*
* @param mDb
* @param clazz
* @param where
* @return 方法事例 :GetDataByWhere(db , Bean.class , "name like '%" +
* searcherFilter + "%'");
*/
public Object GetDataByWhere(SQLiteDatabase mDb, Class clazz, String where) {
Log.d("where:","" + where);
if (cursor != null) {
cursor = null;
}
try {
// Demo SQL Cursor cursor = db.query(TABLE_NAME, null,
// "name like '%" + searcherFilter + "%'", null, null, null, null);
cursor = mDb.query(clazz.getSimpleName(), null, where, null, null,
null, null);
List obList = new ArrayList<Object>();
if (cursor != null) {
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
.moveToNext()) {
Object obj = clazz.newInstance();
for (Field f : clazz.getDeclaredFields()) {
f.setAccessible(true);
// /用反射来调用相应的方法
// /先构造出方法的名字
String CursorGettypeName = f.getType().getSimpleName();
if ("Integer".equals(CursorGettypeName)) {
CursorGettypeName = "int";
} else if ("boolean".equals(CursorGettypeName)) {
CursorGettypeName = "String";
}
CursorGettypeName = CursorGettypeName.substring(0, 1)
.toUpperCase() + CursorGettypeName.substring(1);
String CursorGetName = "get" + CursorGettypeName;
String BeanSetType = f.getName();
String BeanSetName = "set"
+ BeanSetType.substring(0, 1).toUpperCase()
+ BeanSetType.substring(1);
Method BeanSet;
if (f.getType() == boolean.class) {
BeanSet = getBooleanFieldSetMethod(clazz, f);
} else {
BeanSet = clazz.getMethod(BeanSetName, f.getType());
}
Object retValue = cursor.getString(cursor
.getColumnIndex(f.getName()));
Class<?> dataType = f.getType();
if (retValue != null) {
if (dataType == String.class) {
if (retValue != null) {
BeanSet.invoke(obj, retValue.toString());
} else {
BeanSet.invoke(obj, "");
}
} else if (dataType == int.class
|| dataType == Integer.class) {
BeanSet.invoke(
obj,
retValue == null ? (Integer) null
: Integer.parseInt(retValue
.toString()));
} else if (dataType == float.class
|| dataType == Float.class) {
BeanSet.invoke(
obj,
retValue == null ? (Float) null
: Float.parseFloat(retValue
.toString()));
} else if (dataType == double.class
|| dataType == Double.class) {
BeanSet.invoke(
obj,
retValue == null ? (Double) null
: Double.parseDouble(retValue
.toString()));
} else if (dataType == long.class
|| dataType == Long.class) {
BeanSet.invoke(
obj,
retValue == null ? (Long) null : Long
.parseLong(retValue.toString()));
} else if (dataType == boolean.class
|| dataType == Boolean.class) {
BeanSet.invoke(
obj,
retValue == null ? (Boolean) null : "1"
.equals(retValue.toString()));
} else {
BeanSet.invoke(obj, retValue);
}
}
}
obList.add(obj);
obj = null;
}
return obList;
}
} catch (Exception e) {
return null;
} finally {
if (cursor != null)
cursor.close();
cursor = null;
}
return null;
}
public Object GetDataBySQL(SQLiteDatabase mDb, Class clazz, String key) {
if (cursor != null) {
cursor = null;
}
try {
// String sql =
// "select * from (SELECT * from CustomForm where mobile like '%"
// + key
// + "%' order by INSTR(mobile,'"
// + key
// +
// "')) union all select * from (SELECT * from CustomForm where name like '%"
// + key
// + "%' and mobile not like '%"
// + key
// + "%' order by INSTR(name,'" + key + "'))";
String sql = null;
if ("".equals(key)) {
sql = "SELECT * from CustomForm order by name ";
} else {
sql = "SELECT * from CustomForm where mobile like '%" + key
+ "%' or name like '%" + key + "%' or village like '%"
+ key + "%' limit 38";
}
cursor = mDb.rawQuery(sql, null);
List obList = new ArrayList<Object>();
if (cursor != null) {
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
.moveToNext()) {
Object obj = clazz.newInstance();
for (Field f : clazz.getDeclaredFields()) {
f.setAccessible(true);
// /用反射来调用相应的方法
// /先构造出方法的名字
String CursorGettypeName = f.getType().getSimpleName();
if ("Integer".equals(CursorGettypeName)) {
CursorGettypeName = "int";
} else if ("boolean".equals(CursorGettypeName)) {
CursorGettypeName = "String";
}
CursorGettypeName = CursorGettypeName.substring(0, 1)
.toUpperCase() + CursorGettypeName.substring(1);
String CursorGetName = "get" + CursorGettypeName;
String BeanSetType = f.getName();
String BeanSetName = "set"
+ BeanSetType.substring(0, 1).toUpperCase()
+ BeanSetType.substring(1);
Method BeanSet;
if (f.getType() == boolean.class) {
BeanSet = getBooleanFieldSetMethod(clazz, f);
} else {
BeanSet = clazz.getMethod(BeanSetName, f.getType());
}
Object retValue = cursor.getString(cursor
.getColumnIndex(f.getName()));
Class<?> dataType = f.getType();
if (retValue != null) {
if (dataType == String.class) {
if (retValue != null) {
BeanSet.invoke(obj, retValue.toString());
} else {
BeanSet.invoke(obj, "");
}
} else if (dataType == int.class
|| dataType == Integer.class) {
BeanSet.invoke(
obj,
retValue == null ? (Integer) null
: Integer.parseInt(retValue
.toString()));
} else if (dataType == float.class
|| dataType == Float.class) {
BeanSet.invoke(
obj,
retValue == null ? (Float) null
: Float.parseFloat(retValue
.toString()));
} else if (dataType == double.class
|| dataType == Double.class) {
BeanSet.invoke(
obj,
retValue == null ? (Double) null
: Double.parseDouble(retValue
.toString()));
} else if (dataType == long.class
|| dataType == Long.class) {
BeanSet.invoke(
obj,
retValue == null ? (Long) null : Long
.parseLong(retValue.toString()));
} else if (dataType == boolean.class
|| dataType == Boolean.class) {
BeanSet.invoke(
obj,
retValue == null ? (Boolean) null : "1"
.equals(retValue.toString()));
} else {
BeanSet.invoke(obj, retValue);
}
}
}
obList.add(obj);
obj = null;
}
return obList;
}
} catch (Exception e) {
return null;
} finally {
if (cursor != null)
cursor.close();
cursor = null;
}
return null;
}
public Object GetCustomDataBySQL(SQLiteDatabase mDb, Class clazz,
String key, Integer type) {
if (cursor != null) {
cursor = null;
}
try {
// String sql =
// "select * from (SELECT * from CustomForm where mobile like '%"
// + key
// + "%' order by INSTR(mobile,'"
// + key
// +
// "')) union all select * from (SELECT * from CustomForm where name like '%"
// + key
// + "%' and mobile not like '%"
// + key
// + "%' order by INSTR(name,'" + key + "'))";
String sql = null;
if (type == 1) {
if (key == null || "".equals(key)) {
sql = "SELECT * from CustomerListBean limit 100";
} else {
sql = "SELECT * from CustomerListBean where mobile like '%"
+ key + "%' or name like '%" + key + "%' limit 38";
}
} else {
if (key == null || "".equals(key)) {
sql = "SELECT * from CustomerListBean where debt > 0 limit 100";
} else {
sql = "SELECT * from CustomerListBean where debt > 0 and (mobile like '%"
+ key + "%' or name like '%" + key + "%') limit 38";
}
}
// sql = "SELECT * FROM CustomForm WHERE mobile LIKE '"+key+
// "%'union all SELECT * FROM CustomForm WHERE mobile LIKE '_"+key+
// "%'union all SELECT * FROM CustomForm WHERE mobile LIKE '__"+key+
// "%'union all SELECT * FROM CustomForm WHERE mobile LIKE '___"+key+
// "%'union all SELECT * FROM CustomForm WHERE mobile LIKE '____"+key+
// "%'union all SELECT * FROM CustomForm WHERE mobile LIKE '_____"+key+
// "%'union all SELECT * FROM CustomForm WHERE mobile LIKE '______"+key+
// "%'union all SELECT * FROM CustomForm WHERE mobile LIKE '_______"+key+
// "%'union all SELECT * FROM CustomForm WHERE mobile LIKE '________"+key+
// "%'union all SELECT * FROM CustomForm WHERE mobile LIKE '_________"+key+"%'";
cursor = mDb.rawQuery(sql, null);
List obList = new ArrayList<Object>();
if (cursor != null) {
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
.moveToNext()) {
Object obj = clazz.newInstance();
for (Field f : clazz.getDeclaredFields()) {
f.setAccessible(true);
// /用反射来调用相应的方法
// /先构造出方法的名字
String CursorGettypeName = f.getType().getSimpleName();
if ("Integer".equals(CursorGettypeName)) {
CursorGettypeName = "int";
} else if ("boolean".equals(CursorGettypeName)) {
CursorGettypeName = "String";
}
CursorGettypeName = CursorGettypeName.substring(0, 1)
.toUpperCase() + CursorGettypeName.substring(1);
String CursorGetName = "get" + CursorGettypeName;
String BeanSetType = f.getName();
String BeanSetName = "set"
+ BeanSetType.substring(0, 1).toUpperCase()
+ BeanSetType.substring(1);
Method BeanSet;
if (f.getType() == boolean.class) {
BeanSet = getBooleanFieldSetMethod(clazz, f);
} else {
BeanSet = clazz.getMethod(BeanSetName, f.getType());
}
Object retValue = cursor.getString(cursor
.getColumnIndex(f.getName()));
Class<?> dataType = f.getType();
if (retValue != null) {
if (dataType == String.class) {
if (retValue != null) {
BeanSet.invoke(obj, retValue.toString());
} else {
BeanSet.invoke(obj, "");
}
} else if (dataType == int.class
|| dataType == Integer.class) {
BeanSet.invoke(
obj,
retValue == null ? (Integer) null
: Integer.parseInt(retValue
.toString()));
} else if (dataType == float.class
|| dataType == Float.class) {
BeanSet.invoke(
obj,
retValue == null ? (Float) null
: Float.parseFloat(retValue
.toString()));
} else if (dataType == double.class
|| dataType == Double.class) {
BeanSet.invoke(
obj,
retValue == null ? (Double) null
: Double.parseDouble(retValue
.toString()));
} else if (dataType == long.class
|| dataType == Long.class) {
BeanSet.invoke(
obj,
retValue == null ? (Long) null : Long
.parseLong(retValue.toString()));
} else if (dataType == boolean.class
|| dataType == Boolean.class) {
BeanSet.invoke(
obj,
retValue == null ? (Boolean) null : "1"
.equals(retValue.toString()));
} else {
BeanSet.invoke(obj, retValue);
}
}
}
obList.add(obj);
obj = null;
}
return obList;
}
} catch (Exception e) {
return null;
} finally {
if (cursor != null)
cursor.close();
cursor = null;
}
return null;
}
/**
* TODO 更新数据
*
* @param mDb
* @param o
*/
public void Update(SQLiteDatabase mDb, Object o,String where) {
try {
Field[] fields = o.getClass().getDeclaredFields();
ContentValues initialValues = new ContentValues();
for (int i = 0; i < fields.length; i++) {
fields[i].setAccessible(true);
Class<?> dataType = fields[i].getType();
Object value = fields[i].get(o);
if (value != null) {
if (dataType == String.class) {
initialValues.put(fields[i].getName(),
String.valueOf(value));
} else if (dataType == int.class
|| dataType == Integer.class) {
initialValues.put(
fields[i].getName(),
value == null ? (Integer) null : Integer
.parseInt(value.toString()));
} else if (dataType == float.class
|| dataType == Float.class) {
initialValues.put(
fields[i].getName(),
value == null ? (Float) null : Float
.parseFloat(value.toString()));
} else if (dataType == double.class
|| dataType == Double.class) {
initialValues.put(
fields[i].getName(),
value == null ? (Double) null : Double
.parseDouble(value.toString()));
} else if (dataType == long.class
|| dataType == Long.class) {
initialValues.put(
fields[i].getName(),
value == null ? (Long) null : Long
.parseLong(value.toString()));
} else if (dataType == boolean.class
|| dataType == Boolean.class) {
initialValues.put(
fields[i].getName(),
value == null ? (Boolean) null : "1"
.equals(value.toString()));
} else {
initialValues.put(fields[i].getName(),
String.valueOf(value));
}
}
}
mDb.update(o.getClass().getSimpleName(), initialValues, where, null);
} catch (Exception e) {
}
}
/**
* TODO 更新数据
*
* @param mDb
* @param o
*/
public void Update(SQLiteDatabase mDb, Object o) {
try {
Field[] fields = o.getClass().getDeclaredFields();
String where = "";
ContentValues initialValues = new ContentValues();
for (int i = 0; i < fields.length; i++) {
fields[i].setAccessible(true);
Class<?> dataType = fields[i].getType();
Object value = fields[i].get(o);
if (value != null) {
if (KEY_ROWID.equals(fields[i].getName())) {
where = KEY_ROWID + " = " + value;
} else {
if (dataType == String.class) {
initialValues.put(fields[i].getName(),
String.valueOf(value));
} else if (dataType == int.class
|| dataType == Integer.class) {
initialValues.put(
fields[i].getName(),
value == null ? (Integer) null : Integer
.parseInt(value.toString()));
} else if (dataType == float.class
|| dataType == Float.class) {
initialValues.put(
fields[i].getName(),
value == null ? (Float) null : Float
.parseFloat(value.toString()));
} else if (dataType == double.class
|| dataType == Double.class) {
initialValues.put(
fields[i].getName(),
value == null ? (Double) null : Double
.parseDouble(value.toString()));
} else if (dataType == long.class
|| dataType == Long.class) {
initialValues.put(
fields[i].getName(),
value == null ? (Long) null : Long
.parseLong(value.toString()));
} else if (dataType == boolean.class
|| dataType == Boolean.class) {
initialValues.put(
fields[i].getName(),
value == null ? (Boolean) null : "1"
.equals(value.toString()));
} else {
initialValues.put(fields[i].getName(),
String.valueOf(value));
}
}
}
}
mDb.update(o.getClass().getSimpleName(), initialValues, where, null);
} catch (Exception e) {
}
}
/**
* TODO 执行SQL语句
*
* @param mDb
* @param strSQL
*/
public void execSQL(SQLiteDatabase mDb, String strSQL) {
try {
mDb.execSQL(strSQL);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* boolean类型转换方法
*
* @param num
* @return
*/
public boolean changeBoolean(int num) {
if (num == 1) {
return true;
} else {
return false;
}
}
/**
* 获取创建table的SQL语句
*
* @param clazz
*/
public void CreateTable(SQLiteDatabase mDb, Class<?> clazz) {
Field[] fields = clazz.getDeclaredFields();
StringBuffer strSQL = new StringBuffer();
strSQL.append("create table if not exists ");
strSQL.append(clazz.getSimpleName() + " ( " + KEY_ROWID
+ " integer primary key autoincrement");
for (int i = 0; i < fields.length; i++) {
if (!KEY_ROWID.equals(fields[i].getName())) {
strSQL.append("," + fields[i].getName());
}
}
strSQL.append(" ) ;");
mDb.execSQL(strSQL.toString());
Log.d("GGG", "建表语句是-----" + strSQL.toString());
}
/**
* 创建索引
*
* @param mDb
* @param clazz
* @param colums
*/
public void CreateIndex(SQLiteDatabase mDb, Class<?> clazz, String colums) {
StringBuffer strSQL = new StringBuffer();
strSQL.append("create index ");
strSQL.append(colums + "on ");
strSQL.append(clazz.getSimpleName() + "(" + colums + ")");
}
public static Method getBooleanFieldSetMethod(Class<?> clazz, Field f) {
String fn = f.getName();
String mn = "set" + fn.substring(0, 1).toUpperCase() + fn.substring(1);
if (isISStart(f.getName())) {
mn = "set" + fn.substring(2, 3).toUpperCase() + fn.substring(3);
}
try {
return clazz.getDeclaredMethod(mn, f.getType());
} catch (NoSuchMethodException e) {
e.printStackTrace();
return null;
}
}
private static boolean isISStart(String fieldName) {
if (fieldName == null || fieldName.trim().length() == 0)
return false;
// is开头,并且is之后第一个字母是大写 比如 isAdmin
return fieldName.startsWith("is")
&& !Character.isLowerCase(fieldName.charAt(2));
}
}