Android 数据存储方案
利用文件进行存取数据
写入文件数据
openFileOutput 是Context中提供的一个方法,用于将数据存储到指定的文件中,第一个参数是文件名,第二个参数是文件的操作模式。方法的返回一个FileOutputStream对象。
public void saveData(String input) {
FileOutputStream fileOutputStream = null;
BufferedWriter bufferedWriter = null;
try {
fileOutputStream = openFileOutput("data2022", Context.MODE_PRIVATE);
bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
bufferedWriter.write(input);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedWriter != null) {
bufferedWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
读取文件数据
openFileInput是Context中提供的一个方法,用于从文件中读取数据,只有一个参数(文件名称),系统会自动到/data/data/
/files/目录下加载这个文件,并返回一个FileInputStream对象。
public String load() {
FileInputStream in = null;
BufferedReader reader = null;
StringBuilder sb = new StringBuilder();
try {
in = openFileInput("data2022");
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
SharedPreferences存储
键值对存储数据
获取SharedPreferences对象:
1.Context 类中的getSharedPreferences()方法
2.Activity类中的getPreferences()方法
3.PreferenceManager类中的getDefaultSharedPreference()方法
文件生成在/data/data/
/shared_prefs/ 下,文件后缀是xml
findViewById(R.id.button2).setOnClickListener(v -> {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.apply();
});
从生成的文件中取值
findViewById(R.id.button3).setOnClickListener(v -> {
SharedPreferences preferences = getSharedPreferences("data", MODE_PRIVATE);
String name = preferences.getString("name", "");
int age = preferences.getInt("age", 0);
Log.d("MainActivity", "name is :" + name);
Log.d("MainActivity", "age is :" + age);
});
记住密码项目
SharedPreferences preferences1 = getSharedPreferences("re_data", MODE_PRIVATE);
SharedPreferences.Editor editor1 = getSharedPreferences("re_data", MODE_PRIVATE).edit();
EditText userView = findViewById(R.id.user);
EditText passView = findViewById(R.id.pass);
CheckBox check = findViewById(R.id.checkbox);
boolean rember_me = preferences1.getBoolean("rember_me", false);
if (rember_me) {
user = preferences1.getString("user", "");
pass = preferences1.getString("pass", "");
editor1.putBoolean("rember_me", true);
check.setChecked(true);
userView.setText(user);
passView.setText(pass);
}
findViewById(R.id.button4).setOnClickListener(v -> {
user = userView.getText().toString();
pass = passView.getText().toString();
if ("admin".equals(user) && "123".equals(pass)) {
if (check.isChecked()) {
editor1.putString("user", user);
editor1.putString("pass", pass);
editor1.putBoolean("rember_me", true);
editor1.apply();
}else {
editor1.putBoolean("rember_me", false);
editor1.apply();
// editor1.clear();
}
Toast.makeText(this, "已登录", Toast.LENGTH_SHORT).show();
} else {
userView.setText("");
passView.setText("");
editor1.putBoolean("rember_me", false);
editor1.apply();
Toast.makeText(this, "用户名或密码不对,请重新输入", Toast.LENGTH_SHORT).show();
}
});
SQLite数据库储存
创建库
public class MydbHelper extends SQLiteOpenHelper {
public static final String CREATE_BOOK = "create table Book(id integer primary key autoincrement,author text,price real,pages integer,name text)";
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_BOOK);
}
}
mydbHelper = new MydbHelper(this, "BookStore.db", null, 1);
findViewById(R.id.button).setOnClickListener(v -> {
mydbHelper.getWritableDatabase();
});
更新
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists Book");
db.execSQL("drop table if exists Book1");
onCreate(db);
}
增加数据
findViewById(R.id.button1).setOnClickListener(v -> {
SQLiteDatabase db = mydbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name","Thexxx");
values.put("author","tttt");
values.put("pages",900);
values.put("price",10.99);
db.insert("Book",null,values);
});
//也可以直接写SQL
db.execSQL("insert into Book (name,author,pages,price) values(?,?,?,?)",new String[]{"123","123","123","123"});
更新数据
findViewById(R.id.button2).setOnClickListener(v -> {
SQLiteDatabase db = mydbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("author","tom");
db.update("Book",values,"pages < ?",new String[]{"600"});
});
//也可以直接写SQL
db.execSQL("update Book set name =? where price = ?",new String[]{"Tom","123"});
删除数据
findViewById(R.id.button3).setOnClickListener(v -> {
SQLiteDatabase db = mydbHelper.getWritableDatabase();
db.delete("Book","pages < ?",new String[]{"600"});
});
//也可以直接写SQL
db.execSQL("delete from Book where pages < ?",new String[]{"200"});
查询数据
findViewById(R.id.button4).setOnClickListener(v -> {
SQLiteDatabase db = mydbHelper.getWritableDatabase();
Cursor cursor = db.query("Book", null, null, null, null, null, null);
if(cursor.moveToFirst()){
do{
String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
Log.d("MainActivity","book name is " + name);
}while (cursor.moveToNext());
}
cursor.close();
});
//也可以直接写SQL
Cursor cursor = db.rawQuery("select * from Book", null);
LitePal操作数据库
标签:方案,存储,name,db,editor1,new,Android,null,id From: https://www.cnblogs.com/zybao/p/16908171.html** 依赖加载问题,后期补学 **