Android 数据存储

5.1 数据存储方式

文件存储
特点:openFileInput()和openFileOutput()读取设备上的文件。
SharedPreferences
特点:以XML格式将数据存储到设备。
SQLite数据库
特点:运算速度快,占用资源少,还支持基本SQL语法。
ContentProvider
特点:应用程序之间的数据交换,可以将自己的数据共享给其他应用程序使用。
网络存储
特点:通过网络提供的存储空间来存储/获取数据信息。

5.2 文件存储方式

内部存储外部存储
存储位置
将数据以文件的形式存储到应用中
存储位置
将数据以文件的形式存储到外部设备上
存储路径
data/data//目录下
存储位置
mnt/sdcard/目录下。
其它应用操作该文件时需要设置权限其它应用操作该文件时
不用设置权限,会被其它应用共享
删除文件
当应用被卸载时,该文件也会被删除
删除文件
该文件可在本应用外删除,使用前需要确认外部设备是否可用
操作数据
通过openFileOutput()方法和openFileInput()方法获取FileOutputStream和FileInputStream操作对象
删除文件
直接使用FileOutputStream和FileInputStream操作对象

5.2.1 [内部存储]将数据存入文件中

FileOutputStream fos = openFileOutput(String name, int mode);//打开应用程序中对应的输出流,将数据存储到指定的文件中
FileInputStream fis = openFileInput(String name);//打开应用程序对应的输入流,读取指定文件中的数据

name:文件名
mode:文件的操作模式
mode取值:
MODE_PRIVATE:该文件只能被当前程序读写
MODE_APPEND:该文件的内容可以追加; 
MODE_WORLD_READABLE:该文件的内容可以被其他程序读; 
MODE_WORLD_WRITEABLE:该文件的内容可以被其他程序写    
 
    
String fileName = "data.txt";// 文件名称
String content = "helloworld";// 保存数据
FileOutputStream fos = openFileOutput(fileName, MODE_PRIVATE);
fos.write(content.getBytes());将数据写入文件中
fos.close();      

注意:Android系统有一套自己的安全模型,默认情况下任何应用创建的文件都是私有的,其他程序无法访问。

5.2.1 [内部存储]从文件中读取数据

String content = "";
FileInputStream fis = openFileInput("data.txt");//获得文件输入流对象
byte[] buffer = new byte[fis.available()];//创建缓冲区,并获取文件长度
fis.read(buffer);//将文件内容读取到buffer缓冲区        
content = new String(buffer);//转换成字符串
fis.close();//关闭输入流

5.2.2 [外部存储]将数据存入文件中

String state = Environment.getExternalStorageState();//获取外部设备的状态      
if (state.equals(Environment.MEDIA_MOUNTED)) {//判断外部设备是否可用       
    File SDPath = Environment.getExternalStorageDirectory();//获取SD卡目录
    File file = new File(SDPath, "data.txt");
    String data = "HelloWorld";
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(data.getBytes());
    fos.close();
}

5.2.2 [外部存储]从文件中读取数据

String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
    File SDPath = Environment.getExternalStorageDirectory(); //获取SD卡路径
    File file = new File(SDPath, "data.txt");//创建文件对象
    FileInputStream fis = null;
    BufferedReader br = null;
    fis = new FileInputStream(file);//创建文件输入流对象
    //创建字符输入缓冲流的对象
    br = new BufferedReader(new InputStreamReader(fis));
   String data = br.readLine();//读取数据
   br.close();//关闭字符输入缓冲流
   fis.close();//关闭输入流
}

关于权限申请

动态申请SD卡的写权限

在清单文件(AndroidManifest.xml)的<manifest>节点中声明需要

<uses-permission  android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>   

ActivityCompat.requestPermissions(MainActivity.this, new String[]{"android.permission.WRITE_EXTERNAL_STORAGE"}, 1);

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, 
int[] grantResults) {
   	super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

静态申请权限

在清单文件(AndroidManifest.xml)的<manifest>节点中声明需要

<uses-permission  android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission  android:name="android.permission.READ_EXTERNAL_STORAGE"/> 
<uses-permission  android:name=“android.permission. MOUNT_UNMOUNT_FILESYSTEMS”/>//创建与删除文件的权限
 

5.3 SharedPreferences存储

5.3.1将数据存入SharedPreferences

SharedPreferences:是Android平台上一个轻量级的存储类,用于程序中一些少量数据持久化存储

SharedPreferences sp = getSharedPreferences("data",MODE_PRIVATE);//获取编辑器
SharedPreferences.Editor editor = sp.edit();//获取SharedPreferences实例对象
//(value值只能是float、int、long、boolean、String、Set<String>类型数据)
editor.putString("name", “tom");
editor.putInt("age", 8);//存入int类型数据
editor.commit(); 

5.3.1 读取SharedPreferences文件中的数据

SharedPreferences sp = getSharedPreferences("data",MODE_PRIVATE);
String data= sp.getString("name","");      
int  age= sp.getInt(“age",20);           

5.3.2 删除SharedPreferences文件中的数据

editor.remove("name");//根据key删除数据
editor.clear();//删除所有数据
  1. 获取数据的key值与存入数据的key值数据类型要一致,否则查找不到指定数据。

  2. 保存SharedPreferences的key值时,使用静态变量保存,以免操作时写错,如

    private final String key = “username”

实战代码[保存账号和密码]

核心代码

1. [JAVA]SPSave
public class SPSave{
    // 保存QQ账号和登录密码到data.xml文件中
    public static boolean saveUserInfo(Context context, String account,
                                       String password) {
        SharedPreferences sp = context.getSharedPreferences("data",
                Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString("userName", account);
        edit.putString("pwd", password);
        edit.commit();
        return true;
    }
    //从data.xml文件中获取存储的QQ账号和密码
    public static Map<String, String> getUserInfo(Context context) {
        SharedPreferences sp = context.getSharedPreferences("data",
                Context.MODE_PRIVATE);
        String account = sp.getString("userName", null);
        String password = sp.getString("pwd", null);
        Map<String, String> userMap = new HashMap<String, String>();
        userMap.put("account", account);
        userMap.put("password", password);
        return userMap;
    }
}
2.[JAVA] FileSave
public class FileSave {
    //保存QQ账号和登录密码到data.txt文件中
    public static boolean saveUserInfo(Context context, String account, String
            password) {
        FileOutputStream fos = null;
        try {
            //获取文件的输出流对象fos
            fos = context.openFileOutput("data.txt",
                    Context.MODE_PRIVATE);
            //将数据转换为字节码的形式写入data.txt文件中
            fos.write((account + ":" + password).getBytes());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            try {
                if(fos != null){
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //从data.txt文件中获取存储的QQ账号和密码
    public static Map<String, String> getUserInfo(Context context) {
        String content = "";
        FileInputStream fis = null;
        try {
            //获取文件的输入流对象fis
            fis = context.openFileInput("data.txt");
            //将输入流对象中的数据转换为字节码的形式
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);//通过read()方法读取字节码中的数据
            content = new String(buffer); //将获取的字节码转换为字符串
            Map<String, String> userMap = new HashMap<String, String>();
            String[] infos = content.split(":");//将字符串以“:”分隔后形成一个数组的形式
            userMap.put("account", infos[0]);   //将数组中的第一个数据放入userMap集合中
            userMap.put("password", infos[1]); //将数组中的第二个数据放入userMap集合中
            return userMap;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            try {
                if(fis != null){
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
3. [JAVA]MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText et_account;   //账号输入框
    private EditText et_password;  //密码输入框
    private Button btn_login;       //登录按钮
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //通过工具类FileSaveQQ中的getUserInfo()方法获取QQ账号和密码信息
        //Map<String, String> userInfo = FileSaveQQ.getUserInfo(this);
        Map<String, String> userInfo = SPSaveQQ.getUserInfo(this);
        if (userInfo != null) {
            et_account.setText(userInfo.get("account"));   //将获取的账号显示到界面上
            et_password.setText(userInfo.get("password")); //将获取的密码显示到界面上
        }
    }
    private void initView() {
        et_account =  findViewById(R.id.et_account);
        et_password =  findViewById(R.id.et_password);
        btn_login = findViewById(R.id.btn_login);
        //设置按钮的点击监听事件
        btn_login.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                //当点击登录按钮时,获取界面上输入的QQ账号和密码
                String account = et_account.getText().toString().trim();
                String password = et_password.getText().toString();
                //检验输入的账号和密码是否为空
                if (TextUtils.isEmpty(account)) {
                    Toast.makeText(this, "请输入QQ账号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                //保存用户信息
                //boolean isSaveSuccess = FileSaveQQ.saveUserInfo(this, account,password);
                boolean isSaveSuccess = SPSaveQQ.saveUserInfo(this, account, password);
                if (isSaveSuccess) {
                    Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}

5.4 SQLite数据库存储

SQLite是Android自带的一个轻量级的数据库,他运算速度快,占用资源少,支持基本SQL语法。

SQLite数据库可以存储应用程序中的大量数据,并对数据进行管理和维护。

SQLite支持NULL,INTEGER, REAL , TEXT , BLOB五种数据类型,也接收varchar(n), char(n), decimal(p,s )等数据类型。

//SQLiteOpenHelper用于对数据库进行创建和升级操作。
SQLiteOpenHelper(Context context, String name,   SQLiteDatabase.CursorFactory factory, int version)
onCreate(SQLiteDatabase db)
onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
getReadableDatabase()
getWritableDatabase()

5.4.1 创建数据库

public class MyHelper extends SQLiteOpenHelper {
    public MyHelper(Context context) {
        super(context, "caiyuan.db", null, 2);//上下文,数据库名称,游标,数据库版本
    }
    //第一次创建时调用,用于初始化表结构
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE information(_id INTEGER PRIMARY 
                   KEY AUTOINCREMENT, name VARCHAR(20), price INTEGER)");
    }
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
}

5.4.2 SQLiteDatabase

insert(String table, String nullColumnHack, ContentValues values)
delete(String table, String whereClause, String[] whereArgs)
update(String table, ContentValues values, String whereClause, String[] whereArgs)
query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) 
rawQuery(String sql, String[] selectionArgs) 
execSQL(String sql, Object[] bindArgs)
close( )
    
//
添加数据
public void insert(String name,String price) {
    MyHelper helper = new MyHelper(MainActivity.this);
    SQLiteDatabase db = helper.getWritableDatabase();
    ContentValues values = new ContentValues(); 
    values.put("name", name); 
    values.put("price", price);
    long id = db.insert("information",null,values); 
    db.close();                                            
}  
删除数据
public int delete(long id){	
    SQLiteDatabase db = helper.getWritableDatabase();
    int number = db.delete("information", "_id=?", new String[]{id+""});
    db.close();
    return number;
}
修改数据
public int update(String name, String price) {	
    SQLiteDatabase db = helper.getWritableDatabase();
    ContentValues values = new ContentValues(); 
    values.put("price", price); 
    int number = db.update("information", values, " name =?", new String[]{name}); 
    db.close();
    return number;
}
查询数据
public void find(int id){
    MyHelper helper = new MyHelper(MainActivity.this);
    SQLiteDatabase db = helper.getReadableDatabase();
    Cursor cursor = db.query("information", null, "_id=?", new String[]{id+""},null, null, null);
    if (cursor.getCount() != 0){   
    while (cursor.moveToNext()){
    String _id = cursor.getString(cursor.getColumnIndex("_id"));
    String name = cursor.getString(cursor.getColumnIndex("name"));
    String price = cursor.getString(cursor.getColumnIndex("price"));
    }
    }
    cursor.close(); 
    db.close();
}
使用sql语句进行数据库操作
//增加一条数据
db.execSQL("insert into information (name, price) values (?,?)", new Object[]{name, price });
//删除一条数据
db.execSQL("delete from information where _id  = 1");
//修改一条数据
db.execSQL("update information set name=? where price =?", new Object[]{name, price });
//执行查询的SQL语句
Cursor cursor = db.rawQuery("select * from information where name=?",new String[]{name});

5.4.2 QLite中的事务

PersonSQLiteOpenHelper helper = new PersonSQLiteOpenHelper (getApplication());
    SQLiteDatabase db = helper.getWritableDatabase();
    db.beginTransaction();//开启数据库事务
    try {
    db.execSQL("update person set account = account-1000 where name =?", 
    new Object[] { "张三" });
    db.execSQL("update information set account = account +1000 where name =?", 
    new Object[] { "王五" });
    db.setTransactionSuccessful();//设置事务标志为成功,当事务结束时,提交事务
    }catch (Exception e) {
        Log.i("事务处理失败", e.toString());
    } finally {
        db.endTransaction();//关闭数据库事务
        db.close();//关闭数据库
}

实战演练[通讯录]

核心代码


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    MyHelper myHelper;
    private EditText mEtName;
    private EditText mEtPhone;
    private TextView mTvShow;
    private Button mBtnAdd;
    private Button mBtnQuery;
    private Button mBtnUpdate;
    private Button mBtnDelete;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myHelper = new MyHelper(this);
        init();
    }
    private void init() {
        mEtName = findViewById(R.id.et_name);
        mEtPhone = findViewById(R.id.et_phone);
        mTvShow = findViewById(R.id.tv_show);
        mBtnAdd = findViewById(R.id.btn_add);
        mBtnQuery = findViewById(R.id.btn_query);
        mBtnUpdate = findViewById(R.id.btn_update);
        mBtnDelete = findViewById(R.id.btn_delete);
        mBtnAdd.setOnClickListener(this);
        mBtnQuery.setOnClickListener(this);
        mBtnUpdate.setOnClickListener(this);
        mBtnDelete.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        String name, phone;
        SQLiteDatabase db;
        ContentValues values;
        switch (v.getId()) {
            case R.id.btn_add: //添加数据
                name = mEtName.getText().toString();
                phone = mEtPhone.getText().toString();
                db = myHelper.getWritableDatabase();//获取可读写SQLiteDatabse对象
                values = new ContentValues();        //创建ContentValues对象
                values.put("name", name);             //将数据添加到ContentValues对象
                values.put("phone", phone);
                db.insert("information", null, values);
                Toast.makeText(this, "信息已添加", Toast.LENGTH_SHORT).show();
                db.close();
                break;
            case R.id.btn_query: //查询数据
                db = myHelper.getReadableDatabase();
                Cursor cursor = db.query("information", null, null, null, null,
                        null, null);
                if (cursor.getCount() == 0) {
                    mTvShow.setText("");
                    Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
                } else {
                    cursor.moveToFirst();
                    mTvShow.setText("Name :  " + cursor.getString(1) +
                            "  ;Tel :  " + cursor.getString(2));
                }
                while (cursor.moveToNext()) {
                    mTvShow.append("\n" + "Name :  " + cursor.getString(1) +
                            "  ;Tel :  " + cursor.getString(2));
                }
                cursor.close();
                db.close();
                break;
            case R.id.btn_update: //修改数据
                db = myHelper.getWritableDatabase();
                values = new ContentValues();       // 要修改的数据
                values.put("phone", phone = mEtPhone.getText().toString());
                db.update("information", values, "name=?", new String[]{mEtName.getText().toString()}); // 更新并得到行数
                Toast.makeText(this, "信息已修改", Toast.LENGTH_SHORT).show();
                db.close();
                break;
            case R.id.btn_delete: //删除数据
                db = myHelper.getWritableDatabase();
                db.delete("information", null, null);
                Toast.makeText(this, "信息已删除", Toast.LENGTH_SHORT).show();
                mTvShow.setText("");
                db.close();
                break;
        }
    }
    
    class MyHelper extends SQLiteOpenHelper {
        public MyHelper(Context context) {
            super(context, "caiyuan.db", null, 1);
        }
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE information(_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(20),  phone VARCHAR(20))");
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小源同学r

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值