6.1 持久化技术简介
a, 文件存储
b, SharedPreference存储
c, 数据库存储
6.2 文件存储
不对内容不处理的方式,适合存文本数据和二进制文件
6.2.1 将数据存储到文件中
保存数据
在onDestory()方法中加入下面这个方法
public void save(String inputText){
FileOutPutStream out = null;
BufferedWriter writer = null;
try{
out = openFileOutput("data", Context.MODE_PREVATE);
writer = new BufferedWriter(new OutputStream(out));
writer.writer(inputText);
}catch(IOException e){
e.printStackTrace();
}finally{
if(writer != null)
writer.close;
}
}
恢复数据
在onCreate方法中加入
String input = load();
if(!TextUtils.isEmpty(inputText)){
edit.setText(inpurtext);
}
load方法
public String load(){
FileInputStream in = null;
BufferedReader reader = null;
StringBuilder content = null;
try{
in = openFileInput("data");
reader = new BufferReader(new InputStreamReader(in));
String line = "";
while((line = reader.readLine()) != null)
content.append;
}catch(IOException){
e.printStackTrace;
}finally{
if(reader!=null){
reader.close();
}
}
return content.toString;
}
6.3 sharedPreferences存储
存储数据
// 第一个参数表示文件名字,第二个参数表示模式,默认是覆盖
SharedPreferences.Editor editor = getSharedPreferences("massage_item_data", MODE_PRIVATE).edit();
editor.putString("get_massage_item_information", meFragmentFriendItem.getName());
获取数据
SharedPreferences pref = getSharedPreferences("massage_item_data", Context.MODE_PRIVATE);
String res = pref.getString("get_massage_item_information", "");
6.4 SQLite 数据库存储
6.4.1 创建数据库
public static final String CREATE_BOOK = "create table Book("
+ " id integer primary key autoincrement, "
+ " author text, "
+ "price real, "
+ "pages integer"
+ "name text)";
创建如下文件
public class MyDatabaseHelper extends SQLiteOpenHelper {
public static final String CREATE_BOOK = "create table Book("
+ " id integer primary key autoincrement, "
+ " author text, "
+ "price real, "
+ "pages integer,"
+ "name text)";
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);
Toast.makeText(mContext, "Create succeeded", Toast.LENGTH_LONG).show();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public class MainActivity extends AppCompatActivity {
private MyDatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, 1);
Button button_1 = (Button) findViewById(R.id.button_1);
button_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbHelper.getWritableDatabase();
}
});
}
}
6.4.2 升级数据库
修改onUpgrade中的代码
public class MyDatabaseHelper extends SQLiteOpenHelper {
public static final String CREATE_BOOK = "create table Book("
+ " id integer primary key autoincrement, "
+ " author text, "
+ "price real, "
+ "pages integer,"
+ "name text)";
public 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 succeeded", Toast.LENGTH_LONG).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);
}
}
修改MainActivity中的代码
dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, 2);
6.4.3 添加数据
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", "The DaVinci Code");
values.put("author", "Dan Brown");
values.put("pages", 454);
values.put("price", 16.96);
db.insert("Book", null, values);
values.clear();
values.put("name", "The Lost Symbol");
values.put("author", "Dan Brown");
values.put("pages", 519);
values.put("price", 19.95);
db.insert("Book", null, values);
6.4.4 更新数据
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("price", 10.96);
db.update("Book",values, "name = ?", new String[]{
"The DaVinci Code"
});
6.4.5 删除数据
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete("Book", "page > ?", new String[]{"500"});
6.4.6 查询数据
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.query("Book", null, null, null, null, null, null);
if(cursor.moveToFirst()){
do{
String name = cursor.getString(cursor.getColumnIndex("name"));
String author = cursor.getString(cursor.getColumnIndex("author"));
int pages = cursor.getInt(cursor.getColumnIndex("pages"));
Double price = cursor.getDouble(cursor.getColumnIndex("price"));
Log.d("MainActivity", "Book name is " + name);
Log.d("MainActivity", "Book name is " + author);
Log.d("MainActivity", "Book name is " + pages);
Log.d("MainActivity", "Book name is " + price);
}while(cursor.moveToNext());
}
}
6.4.7 使用SQL操作数据库
db.execSQL("insert into Book (name, author, pages, price) values(?, ?, ?, ?)", new String[]{"The Da Vinci code", "Dan Brown", "454", "16.96"} );
db.execSQL("update Book set price = ? where name = ? ", new String[]{"10.99", "The Da Vinci Code"});
db.execSQL("delete from Book where pages > ?", new String[] {"500"});
db.rawQuery(" select * from Book ", null);
效果同上