数据库部分

数据库部分,也要新建一个包,存放数据库的相关类

MyDbOpeanHelper

package com.redcircle.redcircle.sql;

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

import com.redcircle.redcircle.AppApplication;

/**
 * Created by Administrator on 2015/10/10.
 */
public class MyDbOpeanHelper extends SQLiteOpenHelper {
    public MyDbOpeanHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }
    public MyDbOpeanHelper(){
        this(new AppApplication().getmContext(),"SALSE.DB",null,1);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("create table if not exists"+TableConfig.TABLE_WORK_DAILY+"("+TableConfig.WorkDaily.COLUMN_ID+")");
        sqLiteDatabase.execSQL("insert into"+TableConfig.TABLE_WORK_DAILY+"(use_id,type,content,time)values(2,2,nihao,123456)");
        sqLiteDatabase.execSQL("insert into"+TableConfig.TABLE_WORK_DAILY+"(use_id,type,content,time)values(2,2,nihao,123456)");
        sqLiteDatabase.execSQL("insert into"+TableConfig.TABLE_WORK_DAILY+"(use_id,type,content,time)values(2,2,nihao,123456)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}

DbManager

数据库的操作类

package com.redcircle.redcircle.sql;

import android.database.sqlite.SQLiteDatabase;

/**
 * Created by Administrator on 2015/10/10.
 */
public class DbManager {
    //单例,简化数据库操作
    private MyDbOpeanHelper helper;
    private SQLiteDatabase db;
    private DbManager(){
        helper = new MyDbOpeanHelper();
        db = helper.getWritableDatabase();
    }
    private static DbManager manager;
    public static DbManager newInstance(){
        if (manager ==null){
            manager = new DbManager();
        }
        return manager;
    }

    public SQLiteDatabase getDb() {
        return db;
    }
}

TableOperate

表的增删改查
这是个人写的方法,也可用封装好的DbUtils.

package com.redcircle.redcircle.sql;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.umeng.message.proguard.T;

import org.rhq.core.db.DbUtil;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/10/10.
 */
public class TableOperate {
    public  ListgetList(String tableName,ClassentityType){
        SQLiteDatabase db = DbManager.newInstance().getDb();
        Cursor cursor = db.rawQuery("select * from"+tableName+"",null);
        cursor.moveToFirst();
        ArrayListlist = new ArrayList<>();
        while (!cursor.isAfterLast()){
            try {
                T t = entityType.newInstance();
                for (int i = 0;itrue);
                    field.set(t,content);
                    field.setAccessible(false);
                }
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

//    List allWork = new ArrayList<>();
//    DbUtil util = DbUtil
}

TableConfig

存放特定字符

package com.redcircle.redcircle.sql;

/**
 * Created by Administrator on 2015/10/10.
 */
public class TableConfig {
    public static final String TABLE_WORK_DAILY = "work_daily";
    static class WorkDaily{
        public static final String COLUMN_ID = "id";
    }
}

dao设计模式

写一个包dao,放一个类WorkDaily,给出数据库的所有属性,给出getter和setter方法
WorkDaily是一个包含属性和表中字段完全对应的类,并在该类中提供setter和getter方法来设置并获取该类中的属性

package com.redcircle.redcircle.dao;

/**
 * Created by Administrator on 2015/10/10.
 */
public class WorkDaily {
    private String id;
    private String user_id;
    private String type;
    private String content;
    private String time;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUser_id() {
        return user_id;
    }

    public void setUser_id(String user_id) {
        this.user_id = user_id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }
}

你可能感兴趣的:(记录)