Android_SqLite数据库的创建与使用

一:数据库的创建:DBHelper

public class DBHelper extends SQLiteOpenHelper {

    private static final int VERSION = 1;
    private static String DB_NAME = "_name.db";
   
    private String CREATE_TABLE_PERSONS = "create table persons(_id integer primary key,prohibition integer,caseId integer,user_id integer, case_member_id integer,truename text,sex integer,mobile text," +
            "occupation text,birthday text,paper_type integer,paper_num text,employer text,address text,role integer," +
            "relation_member_id integer,type_name text,licence_type text,licence_num text,hxId text)";
   

    public DBHelper(Context context) {
        super(context, SPUtil.getInstance(context).getUserId() + DB_NAME, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(CREATE_TABLE_PERSONS);
      
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      
        db.execSQL("drop table if exists persons");     
       
        db.execSQL(CREATE_TABLE_PERSONS);
       
    }

    public DBHelper instance;

    public void closeDB() {
        super.close();
    }
}

二:建立工具类PersonDao

public class PersonDao {
    private static final String TAG = "PersonDao";
    Context context;
    SQLiteDatabase db;

    public PersonDao(Context context) {
        this.context = context.getApplicationContext();
        //再此自动创建表
        db = new DBHelper(context).getWritableDatabase();
    }

    public void updateOrInsert(Person person, String caseId) {
        ContentValues values = new ContentValues();
        values.put("caseId", caseId);
        values.put("case_member_id", person.getCase_member_id());
        values.put("truename", person.getTruename());
        values.put("sex", person.getSex());
        values.put("mobile", person.getMobile());
        values.put("occupation", person.getOccupation());
        values.put("birthday", person.getBirthday());
        values.put("paper_type", person.getPaper_type());
        values.put("paper_num", person.getPaper_num());
        values.put("employer", person.getEmployer());
        values.put("address", person.getAddress());
        values.put("role", person.getRole());
        values.put("relation_member_id", person.getRelation_member_id());
        values.put("type_name", person.getType_name());
        values.put("licence_type", person.getLicence_type());
        values.put("licence_num", person.getLicence_num());
        values.put("user_id", person.getUser_id());
        values.put("prohibition", person.getProhibition());
        values.put("hxId", person.getHxId());

        int r = db.update("persons", values, "caseId=? and case_member_id=?", new String[]{caseId, person.getCase_member_id() + ""});
        if (r > 0) {
            L.e("person更新成功:" + values);
            db.close();
        } else {
            values.put("prohibition", 1);
            long x = db.insert("persons", null, values);
            if (x > -1) {
                L.e("person插入成功:" + values);

            }
            db.close();
        }
    }

    public Person queryOnePerson(String caseId, String user_id) {
        Person person = null;
        Cursor cursor = db.query("persons", null, "caseId=? and user_id=?", new String[]{caseId, user_id}, null, null, null);
        while (cursor.moveToNext()) {
            person = new Person();
            person.setCase_member_id(cursor.getInt(cursor.getColumnIndex("case_member_id")));
            person.setTruename(cursor.getString(cursor.getColumnIndex("truename")));
            person.setSex(cursor.getInt(cursor.getColumnIndex("sex")));
            person.setMobile(cursor.getString(cursor.getColumnIndex("mobile")));
            person.setOccupation(cursor.getString(cursor.getColumnIndex("occupation")));
            person.setBirthday(cursor.getString(cursor.getColumnIndex("birthday")));
            person.setPaper_type(cursor.getInt(cursor.getColumnIndex("paper_type")));
            person.setPaper_num(cursor.getString(cursor.getColumnIndex("paper_num")));
            person.setEmployer(cursor.getString(cursor.getColumnIndex("employer")));
            person.setAddress(cursor.getString(cursor.getColumnIndex("address")));
            person.setRole(cursor.getInt(cursor.getColumnIndex("role")));
            person.setRelation_member_id(cursor.getInt(cursor.getColumnIndex("relation_member_id")));
            person.setType_name(cursor.getString(cursor.getColumnIndex("type_name")));
            person.setLicence_type(cursor.getString(cursor.getColumnIndex("licence_type")));
            person.setLicence_num(cursor.getString(cursor.getColumnIndex("licence_num")));
            person.setUser_id(cursor.getInt(cursor.getColumnIndex("user_id")));
            person.setProhibition(cursor.getInt(cursor.getColumnIndex("prohibition")));
        }
        cursor.close();
        return person;
    }

    public ArrayList<Person> queryPersonByCaseId(String case_id) {//这个地方有bug,这样查询会查询出来很多个person,并且每个person会在不同的案件中有不同的身份
        Cursor cursor = db.query("persons", null, "caseId=?", new String[]{case_id}, null, null, null);
        System.out.println("查询数据库" + cursor.toString());
        ArrayList<Person> personsList = new ArrayList<>();
        while (cursor.moveToNext()) {
            System.out.println("查询数据库666" + cursor.toString());
            Person person = new Person();
            person.setCase_member_id(cursor.getInt(cursor.getColumnIndex("case_member_id")));
            person.setTruename(cursor.getString(cursor.getColumnIndex("truename")));
            person.setSex(cursor.getInt(cursor.getColumnIndex("sex")));
            person.setMobile(cursor.getString(cursor.getColumnIndex("mobile")));
            person.setOccupation(cursor.getString(cursor.getColumnIndex("occupation")));
            person.setBirthday(cursor.getString(cursor.getColumnIndex("birthday")));
            person.setPaper_type(cursor.getInt(cursor.getColumnIndex("paper_type")));
            person.setPaper_num(cursor.getString(cursor.getColumnIndex("paper_num")));
            person.setEmployer(cursor.getString(cursor.getColumnIndex("employer")));
            person.setAddress(cursor.getString(cursor.getColumnIndex("address")));
            person.setRole(cursor.getInt(cursor.getColumnIndex("role")));
            person.setRelation_member_id(cursor.getInt(cursor.getColumnIndex("relation_member_id")));
            person.setType_name(cursor.getString(cursor.getColumnIndex("type_name")));
            person.setLicence_type(cursor.getString(cursor.getColumnIndex("licence_type")));
            person.setLicence_num(cursor.getString(cursor.getColumnIndex("licence_num")));
            person.setUser_id(cursor.getInt(cursor.getColumnIndex("user_id")));
            person.setProhibition(cursor.getInt(cursor.getColumnIndex("prohibition")));
            person.setHxId(cursor.getString(cursor.getColumnIndex("hxId")));
            personsList.add(person);
        }
        cursor.close();
        db.close();
        return personsList;
    }

    public Person queryPersonByHXid(String caseId,String hxId) {
        Person person = null;
        Cursor cursor = db.query("persons", null, "caseId=? and hxId=?", new String[]{caseId,hxId}, null, null, null);
        while (cursor.moveToNext()) {
            person = new Person();
            person.setCase_member_id(cursor.getInt(cursor.getColumnIndex("case_member_id")));
            person.setTruename(cursor.getString(cursor.getColumnIndex("truename")));
            person.setSex(cursor.getInt(cursor.getColumnIndex("sex")));
            person.setMobile(cursor.getString(cursor.getColumnIndex("mobile")));
            person.setOccupation(cursor.getString(cursor.getColumnIndex("occupation")));
            person.setBirthday(cursor.getString(cursor.getColumnIndex("birthday")));
            person.setPaper_type(cursor.getInt(cursor.getColumnIndex("paper_type")));
            person.setPaper_num(cursor.getString(cursor.getColumnIndex("paper_num")));
            person.setEmployer(cursor.getString(cursor.getColumnIndex("employer")));
            person.setAddress(cursor.getString(cursor.getColumnIndex("address")));
            person.setRole(cursor.getInt(cursor.getColumnIndex("role")));
            person.setRelation_member_id(cursor.getInt(cursor.getColumnIndex("relation_member_id")));
            person.setType_name(cursor.getString(cursor.getColumnIndex("type_name")));
            person.setLicence_type(cursor.getString(cursor.getColumnIndex("licence_type")));
            person.setLicence_num(cursor.getString(cursor.getColumnIndex("licence_num")));
            person.setUser_id(cursor.getInt(cursor.getColumnIndex("user_id")));
            person.setProhibition(cursor.getInt(cursor.getColumnIndex("prohibition")));
            person.setHxId(cursor.getString(cursor.getColumnIndex("hxId")));
        }
        cursor.close();
        db.close();
        return person;
    }

    public ArrayList<Person> queryPersonsByCaseId(String caseId) {
        ArrayList<Person> list = new ArrayList<>();
        Cursor cursor = db.query(true, "persons", null, "caseId=?", new String[]{caseId}, null, null, null, null);
        Person person;
        while (cursor.moveToNext()) {
            person = new Person();
            person.setCase_member_id(cursor.getInt(cursor.getColumnIndex("case_member_id")));
            person.setTruename(cursor.getString(cursor.getColumnIndex("truename")));
            person.setSex(cursor.getInt(cursor.getColumnIndex("sex")));
            person.setMobile(cursor.getString(cursor.getColumnIndex("mobile")));
            person.setOccupation(cursor.getString(cursor.getColumnIndex("occupation")));
            person.setBirthday(cursor.getString(cursor.getColumnIndex("birthday")));
            person.setPaper_type(cursor.getInt(cursor.getColumnIndex("paper_type")));
            person.setPaper_num(cursor.getString(cursor.getColumnIndex("paper_num")));
            person.setEmployer(cursor.getString(cursor.getColumnIndex("employer")));
            person.setAddress(cursor.getString(cursor.getColumnIndex("address")));
            person.setRole(cursor.getInt(cursor.getColumnIndex("role")));
            person.setRelation_member_id(cursor.getInt(cursor.getColumnIndex("relation_member_id")));
            person.setType_name(cursor.getString(cursor.getColumnIndex("type_name")));
            person.setLicence_type(cursor.getString(cursor.getColumnIndex("licence_type")));
            person.setLicence_num(cursor.getString(cursor.getColumnIndex("licence_num")));
            person.setUser_id(cursor.getInt(cursor.getColumnIndex("user_id")));
            person.setProhibition(cursor.getInt(cursor.getColumnIndex("prohibition")));
            person.setHxId(cursor.getString(cursor.getColumnIndex("hxId")));
            if (!list.contains(person))
                list.add(person);
        }
        cursor.close();
        db.close();
        return list;
    }

    public void deleteByCaseId(String caseId) {
        db.delete("persons", "caseId=?", new String[]{caseId});
    }

    public String getPersonRole(String caseId, String case_member_id) {
        String role = null;
        String sql = "select role from persons where caseId='" + caseId + "' and case_member_id='" + case_member_id + "'";
        L.e("sql->" + sql);
        Cursor cursor = db.query("persons", new String[]{"role"}, "caseId=? and case_member_id=?", new String[]{caseId, case_member_id}, null, null, null);
        while (cursor.moveToNext()) {
            role = cursor.getString(0);
        }
        cursor.close();
        db.close();
        return role;
    }

    public void updatePersonPermision(String caseId, String userId, int permision) {
        ContentValues values = new ContentValues();
        values.put("prohibition", permision);
        int r = db.update("persons", values, "caseId=? and user_id=?", new String[]{caseId, userId});
        L.e("PersonDao", "更新结果->" + r);
        db.close();
    }

    public void release() {
        if (db != null) {
            db.close();
        }
    }
}


三:person的bean类

package com.zjtp.dyhtj.anjian.modle.beans;

/**
 * 人员实体类(注重关注于应用的业务逻辑)
 * Created by Administrator on 2017/7/17.
 */

public class Person {

    public void Person() {
    }


    public static final int ROLE_TJY = 0;
    public static final int ROLE_YG = 1;
    public static final int ROLE_YGDLR = 2;
    public static final int ROLE_BG = 3;
    public static final int ROLE_BGDLR = 4;
    public static final int ROLE_BGF = 6;
    public static final int ROLE_YGF = 7;

    /**
     * case_member_id : 67
     * truename : 李丰君代理
     * sex : 1
     * mobile : 13311167851
     * occupation :
     * birthday : 1992-11-11
     * paper_type : 1
     * paper_num : 371322199211110016
     * employer : 律金汇
     * address : 测试
     * role : 1
     * relation_member_id : 66
     * type_name : 自然人
     * licence_type :
     * licence_num :
     */

    public String hxId;
    public int user_id;
    public int prohibition;
    public int case_member_id;
    public String truename;
    public int sex;
    public String mobile;
    public String occupation;
    public String birthday;
    public int paper_type;
    public String paper_num;
    public String employer;
    public String address;
    public int role;
    public int relation_member_id;
    public String type_name;
    public String licence_type;
    public String licence_num;

    public String getHxId() {
        return hxId;
    }

    public void setHxId(String hxId) {
        this.hxId = hxId;
    }

    public int getProhibition() {
        return prohibition;
    }

    public void setProhibition(int prohibition) {
        this.prohibition = prohibition;
    }

    public int getUser_id() {
        return user_id;
    }

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

    public int getCase_member_id() {
        return case_member_id;
    }

    public void setCase_member_id(int case_member_id) {
        this.case_member_id = case_member_id;
    }

    public String getTruename() {
        return truename;
    }

    public void setTruename(String truename) {
        this.truename = truename;
    }

    public int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getOccupation() {
        return occupation;
    }

    public void setOccupation(String occupation) {
        this.occupation = occupation;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public int getPaper_type() {
        return paper_type;
    }

    public void setPaper_type(int paper_type) {
        this.paper_type = paper_type;
    }

    public String getPaper_num() {
        return paper_num;
    }

    public void setPaper_num(String paper_num) {
        this.paper_num = paper_num;
    }

    public String getEmployer() {
        return employer;
    }

    public void setEmployer(String employer) {
        this.employer = employer;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getRole() {
        return role;
    }

    public void setRole(int role) {
        this.role = role;
    }

    public int getRelation_member_id() {
        return relation_member_id;
    }

    public void setRelation_member_id(int relation_member_id) {
        this.relation_member_id = relation_member_id;
    }

    public String getType_name() {
        return type_name;
    }

    public void setType_name(String type_name) {
        this.type_name = type_name;
    }

    public String getLicence_type() {
        return licence_type;
    }

    public void setLicence_type(String licence_type) {
        this.licence_type = licence_type;
    }

    public String getLicence_num() {
        return licence_num;
    }

    public void setLicence_num(String licence_num) {
        this.licence_num = licence_num;
    }

    @Override
    public String toString() {
        return "Person{" +
                "hxId='" + hxId + '\'' +
                ", user_id=" + user_id +
                ", prohibition=" + prohibition +
                ", case_member_id=" + case_member_id +
                ", truename='" + truename + '\'' +
                ", sex=" + sex +
                ", mobile='" + mobile + '\'' +
                ", occupation='" + occupation + '\'' +
                ", birthday='" + birthday + '\'' +
                ", paper_type=" + paper_type +
                ", paper_num='" + paper_num + '\'' +
                ", employer='" + employer + '\'' +
                ", address='" + address + '\'' +
                ", role=" + role +
                ", relation_member_id=" + relation_member_id +
                ", type_name='" + type_name + '\'' +
                ", licence_type='" + licence_type + '\'' +
                ", licence_num='" + licence_num + '\'' +
                '}';
    }
}

四:插入数据库:

 //把原告代理人信息存入数据库
                        Person person = new Person();
                        person.setCase_member_id(accuserAgentBean.getCase_member_id());
                        person.setTruename(accuserAgentBean.getTruename());
                        person.setSex(accuserAgentBean.getSex());
                        person.setMobile(accuserAgentBean.getMobile());
                        person.setOccupation(accuserAgentBean.getOccupation());
                        person.setBirthday(accuserAgentBean.getBirthday().toString());
                        person.setPaper_type(accuserAgentBean.getPaper_type());
                        person.setPaper_num(accuserAgentBean.getPaper_num());
                        person.setEmployer(accuserAgentBean.getEmployer());
                        person.setAddress(accuserAgentBean.getAddress());
                        person.setRole(accuserAgentBean.getRole());
                        person.setRelation_member_id(accuserAgentBean.getRelation_member_id());
                        person.setType_name(accuserAgentBean.getTruename());
                        person.setLicence_type(accuserAgentBean.getLicence_type());
                        person.setLicence_num(accuserAgentBean.getLicence_num());
                        person.setUser_id(accuserAgentBean.getUser_id());
                        person.setHxId(accuserAgentBean.getHx_account());
                        PersonDao personDao = new PersonDao(getActivity());
                        person.setProhibition(0);
                        personDao.updateOrInsert(person, case_id + "");

五:查询数据库:

  //查询表里是否有数据
  final ArrayList<Person> persons = personDao.queryPersonsByCaseId(case_id + "");
  if (persons.size() != 0) {
    //有数据
   }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值