本地数据库管理类

自定义继承SQLiteOpenHelper类

import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/** * Created by Administrator on 2015/10/8. */
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
    public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    public MySQLiteOpenHelper(Context context, String name) {
        super(context, name,null,1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //创建表,语句跟基本的SQL语句一样
        db.execSQL("create table if not exists......");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

创建DBManager类,由SQLiteOpenHelper单例得到SQLiteDatabase对象

import android.database.sqlite.SQLiteDatabase;
import com.test.administrator.myproject.BaseApplication;

/** * Created by Administrator on 2015/10/12. */
public class DBManager {
    private MySQLiteOpenHelper helper;
    private SQLiteDatabase db;
    private DBManager(){
        //BaseApplication.getmBaseApplication()传入上下文
        helper = new MySQLiteOpenHelper(BaseApplication.getmBaseApplication(),TableConfig.TABLE_NAME);
        db = helper.getWritableDatabase();
    }
    private static DBManager manager;
    public synchronized static DBManager newInstance(){
        if (manager==null){
            manager=new DBManager();
        }
        return manager;
    }

    public SQLiteDatabase getDb() {
        return db;
    }
}

创建DataBaseOperate类,仅以查询为例,遍历本地数据库数据

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/** * Created by Administrator on 2015/10/11. */
public class DataBaseOperate {

    public static <T> List<T> getList(String tableName,Class<T> entityType ){
        SQLiteDatabase db = DBManager.newInstance().getDb();//获得数据库
        Cursor cursor = db.rawQuery("select * from "+tableName,null);//获得cursor
        cursor.moveToFirst();
        ArrayList<T> list = new ArrayList<>();//创建一个list用于存储表中所有数据
        try {
            while(!cursor.isAfterLast()){
                T t = entityType.newInstance(); //通过反射获得Class对象
                for(int i = 0;i<cursor.getColumnCount();i++)
                {
                    String content = cursor.getString(i);//通过cursor指针获得i列的的name
                    String columnName = cursor.getColumnName(i);//通过cursor指针获得i列名称
                    Field field = entityType.getDeclaredField(columnName);//利用反射来通过列名获得属性
                    field.setAccessible(true);
                    field.set(t, content);//将获得的content值添加到t对象中
                    field.setAccessible(false);

                }
                cursor.moveToNext();
                list.add(t); //将t添加到list中。
            }
            return list;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        /** * 也可以直接使用xUtils中的dbUtils框架 */
        /*List<T> allWork = new ArrayList<>(); DbUtils utils = DbUtils.create(BaseApplication.getmBaseApplication(),tableName); try { allWork = utils.findAll(entityType); } catch (DbException e) { e.printStackTrace(); } return allWork;*/
        return null;
    }
}

你可能感兴趣的:(本地数据库管理)