资讯专栏INFORMATION COLUMN

Android持久化存储总结

Eric / 1814人阅读

摘要:本文旨在复习总结持久化存储方法,只列出了主要代码以及少量批注,仅作为复习参考使用。

本文旨在复习总结Android持久化存储方法,只列出了主要代码以及少量批注,仅作为复习参考使用。

文件存储

所有文件默认放在/data/data//files/目录下

文件写入
    public void save(String inputText){
        FileOutputStream out=null;
        BufferedWriter writer=null;
        try {
            out=openFileOutput("data", Context.MODE_PRIVATE);
            writer=new BufferedWriter(new OutputStreamWriter(out));
            writer.write(inputText);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(writer!=null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
读取文件
    public String load(){
        FileInputStream in =null;
        BufferedReader reader=null;
        StringBuilder content=new StringBuilder();
        try {
            in=openFileInput("data");
            reader=new BufferedReader(new InputStreamReader(in));
            String line="";
            while ((line=reader.readLine())!=null){
                content.append(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return content.toString();
    }
SharePreferences方法

SharePreference文件默认放在/data/data//shared_prefs/目录下

SharedPreferences写入
SharedPreferences.Editor editor=getSharedPreferences("data",MODE_PRIVATE).edit();
editor.putString("et_inputText","sharePreferences test");
editor.commit();
SharedPreferences读取
SharedPreferences sp=getSharedPreferences("data",MODE_PRIVATE);
String input=sp.getString("et_inputText","请输入用户名");//第二个参数是为空的默认信息
SQLite数据库存储

SQLite文件默认放在/data/data//databases/目录下

public class MyDataBaseHelper extends SQLiteOpenHelper{
    private static final String CREATE_BOOK="create table Book("
            +"id integer primary key autoincrement, "
            +"author text, "
            +"price real, "
            +"pages integer, "
            +"name text)";
    private static final String CREATE_CATEGORY="create table Category("
            +"id integer primary key autoincrement, "
            +"category_name text, "
            +"category_code integer)";
    private Context mContext;
    public MyDataBaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        mContext=context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_BOOK);
        db.execSQL(CREATE_CATEGORY);
        Toast.makeText(mContext,"create succeed",Toast.LENGTH_SHORT).show();
    }

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

创建数据库

 dbHelper=new MyDataBaseHelper(this,"BookStore.db",null,2);

插入数据

SQLiteDatabase db=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name","Effective Java");
values.put("author","Joshua Bloch");
values.put("pages",454);
values.put("price",16.96);
db.insert("Book",null,values);

更新数据

ContentValues values=new ContentValues();
values.put("price",198.00);
SQLiteDatabase db=dbHelper.getReadableDatabase();
db.update("Book",values,"name=?",new String[]{"Android Programme"});

删除行

SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete("Book", "pages > ?", new String[] { "500" });

ContentValues知识android提供的一个便于数据库操作的类,为数据库操作提供便利,也可以直接使用数据库语句进行操作

参考书目《第一行代码》

更多关于java的文章请戳这里:(您的留言意见是对我最大的支持)

我的文章列表
Email:sxh13208803520@gmail.com

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66961.html

相关文章

  • 01.Android之基础组件问题

    摘要:此时再次旋转屏幕时,该不会被系统杀死和重建,只会调用。因此可通过和来判断是否被重建,并取出数据进行恢复。但需要注意的是,在取出数据时一定要先判断是否为空。只有在进程不被掉,正常情况下才会执行方法。 目录介绍 1.0.0.1 说下Activity的生命周期?屏幕旋转时生命周期?异常条件会调用什么方法? 1.0.0.2 后台的Activity被系统回收怎么办?说一下onSaveInsta...

    iamyoung001 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<