android 本地存储数据之sqlite

        最近写了一个抽签值日的程序需要用到数据库来存储数据,如果放到数据库中还要建表,还需要连网,本人感觉没有必要,所以就选择了本地存储sqlite的方式,来记录一下,方便以后查看

先创建一个工具类DateBaseHelper

public class StuDBHelper extends SQLiteOpenHelper{
    public final static String NAME_CONTEXT_TAL = "name_context";//项目表
    public final static String NAME_PERSON_TAL = "person_context";//人员与项目对应表
    public final static int DB_VERSION = 6;

    public StuDBHelper(Context context) {
        super(context, NAME_CONTEXT_TAL, null, DB_VERSION);
        // TODO Auto-generated constructor stub
    }
    
    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        String sql = "create table if not exists " + NAME_CONTEXT_TAL + "(_id integer primary key autoincrement,context_id varchar, context_name varchar)";
        db.execSQL(sql);
        String personsql =  "create table if not exists " + NAME_PERSON_TAL + "(_id integer primary key autoincrement,person_id varchar, person_name varchar,context_name varchar)";
        db.execSQL(personsql);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
    }

将数据插入到数据库中:

//将系统消息数据插入到本地数据库
     public void insertmessge(){
         if(messageList != null &&messageList.size() != 0){
             helper = new StuDBHelper(ContentActivity.this);
             SQLiteDatabase db = helper.getWritableDatabase();
            //插入系统消息
             for (int i = 0; i < messageList.size(); i++) {
                 ContentValues valuse = new ContentValues();
                 valuse.put("context_id",messageList.get(i).getContext_id() );
                 valuse.put("context_name",messageList.get(i).getContext_name());
                 db.insert(helper.NAME_CONTEXT_TAL,null, valuse);  
             }
             db.close();
         }
     }

删除数据库中的数据:

helper = new StuDBHelper(ContentActivity.this);
SQLiteDatabase db = helper.getWritableDatabase();
db.delete(helper.NAME_CONTEXT_TAL,"context_id=?",newString[{basicDateEntity.getListstring().get(position).getContext_id()});
db.close();

修改数据库中的数据:

helper = new StuDBHelper(ContentActivity.this);
     SQLiteDatabase db = helper.getWritableDatabase();
     ContentValues value = new ContentValues();
     value.put("code", pro.getCode());
     value.put("name", pro.getName());
     value.put("qty", pro.getQty());
     db.update(helper.NAME_CONTEXT_TAL,value,"context_id=?","context_id=?",newString[{basicDateEntity.getListstring().get(position).getContext_id()})
     db.close();
            

查询数据库中的数据(在这里我写了一个公共类):

public class Countents {
    Context context;
    List<MessageEntity> message_list;
    List<PersonMessageEntity> personmessage_list;
    private static StuDBHelper helper;
    public static SQLiteDatabase db;
    public static Cursor cursor;
    private String context_names;
    private String context_ids;
    private String person_contexts;
    private String person_ids;
    private String person_names;
    BasicDateEntity basicDateEntity = BasicDateEntity.getSingle();
    public Countents(Context context){
        this.context = context;
    }
    public void SQLitedate(){
        message_list = new ArrayList<MessageEntity>();
        helper = new StuDBHelper(context);
        // 通过数据库助手,创建数据库
        db = helper.getReadableDatabase();
        cursor = db.query(StuDBHelper.NAME_CONTEXT_TAL, null, null,null, null, null, null, null);
        // 字段对应的索引值
        int context_name = cursor.getColumnIndex("context_name");
        int context_id = cursor.getColumnIndex("context_id");
        // 遍历查询数据
        while (cursor.moveToNext()) {
            // 通过索引值间接获取字段对应的数据
            context_names = cursor.getString(context_name);
            context_ids = cursor.getString(context_id);
            MessageEntity messagelist = new MessageEntity(context_names,context_ids);
            message_list.add(messagelist);
        }
        basicDateEntity.setListstring(message_list);
        db.close();
        cursor.close();
    }
    public void SQLitedatePerson(){
        personmessage_list = new ArrayList<PersonMessageEntity>();
        helper = new StuDBHelper(context);
        // 通过数据库助手,创建数据库
        db = helper.getReadableDatabase();
        cursor = db.query(StuDBHelper.NAME_PERSON_TAL, null, null,null, null, null, null, null);
        // 字段对应的索引值
        int person_context = cursor.getColumnIndex("context_name");
        int person_name = cursor.getColumnIndex("person_name");
        int person_id = cursor.getColumnIndex("person_id");
        // 遍历查询数据
        while (cursor.moveToNext()) {
            // 通过索引值间接获取字段对应的数据
            person_contexts = cursor.getString(person_context);
            person_ids = cursor.getString(person_id);
            person_names = cursor.getString(person_name);
            PersonMessageEntity personmessage = new PersonMessageEntity(person_ids,person_names,person_contexts);
            personmessage_list.add(personmessage);
        }
        basicDateEntity.setPersonlist(personmessage_list);
        db.close();
        cursor.close();
    }
}

调用此类中方法就可以查询到数据

Countents countents = new Countents(ContentActivity.this);

//调用查询的方法
countents.SQLitedate();

}

仅为自己以后用到该功能提供方便

猜你喜欢

转载自blog.csdn.net/wkh11/article/details/82251416