当前位置:数据库 > SQLite >>

android序列化与反序列话HashMap到sqlite数据库

1.首先给出序列化与反序列化工具类

[html]
public class SerializableInterface { 
    public SerializableInterface(){ 
 
    } 
 
    public static byte[] serialize(HashMap<String, String> hashMap){ 
        try { 
        ByteArrayOutputStream mem_out = new ByteArrayOutputStream(); 
            ObjectOutputStream out = new ObjectOutputStream(mem_out); 
 
            out.writeObject(hashMap); 
 
            out.close(); 
           mem_out.close(); 
 
           byte[] bytes =  mem_out.toByteArray(); 
           return bytes; 
        } catch (IOException e) { 
            return null; 
        } 
    } 
 
     public static HashMap<String, String> deserialize(byte[] bytes){ 
        try { 
            ByteArrayInputStream mem_in = new ByteArrayInputStream(bytes); 
            ObjectInputStream in = new ObjectInputStream(mem_in); 
 
            HashMap<String, String> hashMap = (HashMap<String, String>)in.readObject(); 
 
             in.close(); 
             mem_in.close(); 
 
             return hashMap; 
        } catch (StreamCorruptedException e) { 
            return null; 
        } catch (ClassNotFoundException e) { 
            return null; 
        }   catch (IOException e) { 
            return null; 
        } 
     } 

2.定义一个方法获取学生的信息,其中家庭成员信息保存在hashmap后,序列化到sqlite

[html]
public ArrayList<ContentValues> formatStudentInfo() { 
        ArrayList<ContentValues> valueArr = null; 
        ContentValues values = null; 
        HashMap<String, String> map = null; 
 
        valueArr = new ArrayList<ContentValues>();    
        //第一组数据 www.zzzyk.com  
        values = new ContentValues(3); 
                                     
        values.put("name", "david"); 
        values.put("class","0502"); 
        map = new HashMap<String, String>(); 
        map.put("mom", "lucy"); 
        map.put("dad", "jack"); 
        byte[] bytes = SerializableInterface.serialize(map); 
        values.put("family", bytes); 
        valueArr.add(values); 
         
        //第二组数据 
        values = new ContentValues(3); 
         
        values.put("name", "joy"); 
        values.put("class","0502"); 
        map = new HashMap<String, String>(); 
        map.put("mom", "mary"); 
        map.put("dad", "json"); 
        bytes = SerializableInterface.serialize(map); 
        values.put("family", bytes); 
        valueArr.add(values); 
          
        return valueArr; 
    } 
3.调用数据库的操作类将
[html]
formatStudentInfo返回的数据insert到数据库。 
这个方法应该放在自己写的继承了SQLiteOpenHelper的类中 
    public void insertAll(String databaseName, 
            ArrayList<ContentValues> valuesArr) { 
        SQLiteDatabase db = getWritableDatabase(); 
        db.beginTransaction();  
 
        for (ContentValues val : valuesArr) { 
            db.insert(databaseName, null, val); 
        } 
 
        db.setTransactionSuccessful();  
        db.endTransaction(); 
        db.close(); 
    } 
 
读取的时候调用 
Cursor cursor db.query("fix your self"); 
....... 
//通过getBlob方法获取bytes后反序列化得到map对象,参数x是序列化字段所在的列数(从0开始计数)。 
byte[] bytes = cursor.getBlob(x); 
HashMap<String, String> map = SerializableInterface.deserialize(bytes); 

以上仅是对序列化与反序列化HashMap对象做一个简单的介绍,代码并不完整,数据库操作部分需要自己补充完整。

 


摘自 weidawei0609的专栏

补充:移动开发 , Android ,
Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,