1、使用文件进行存储
public void save(String s){
FileOutputStream out = null;
BufferedWriter writer = null;
try {
out = openFileOutput("data", Context.MODE_PRIVATE);
writer = new BufferedWriter(new OutputStreamWriter(out));
writer.write(s);
}
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 (IOException e) {
e.printStackTrace();
}finally {
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return content.toString();
}
2、SharedPrefernces存储:按键值对存储
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences.Editor editor = getSharedPreferences("data01", MODE_PRIVATE).edit();
editor.putString("name","jmm");
editor.putInt("age",23);
editor.commit();
}
});
从SharedPrefernces取出数据
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences pref = getSharedPreferences("data01", MODE_PRIVATE);
String name = pref.getString("name","");
int age = pref.getInt("age",0);
Log.e("0726", "name is: "+name);
Log.e("0726", "age is:"+age);
}
});
3、使用Sqlite持久化数据 创建MyDatabaseHelper类继承自SQLiteOpenHelper,重写父类两个方法及创建构造方法
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_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("drop table if exists Book");
db.execSQL("drop table if exists Category");
onCreate(db);
}
创建数据库:
myDatabaseHelper = new MyDatabaseHelper(this, "BookStore.db", null, 2);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myDatabaseHelper.getWritableDatabase();
}
});
增加数据:
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SQLiteDatabase db = myDatabaseHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name","Android第一行代码");
values.put("author","郭霖");
values.put("pages",552);
values.put("price",79);
db.insert("Book",null,values);
values.clear();
}
});
删除数据:
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SQLiteDatabase db = myDatabaseHelper.getWritableDatabase();
db.delete("Book","pages > ?",new String[]{"500"});
}
});
修改数据:
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SQLiteDatabase db = myDatabaseHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("price",59);
db.update("Book",values,"name = ?",new String[]{"Android第一行代码"});
}
});
查询数据:
query.setOnClickListener(new View.OnClickListener() {
private static final String TAG = "0726";
@Override
public void onClick(View view) {
SQLiteDatabase db = myDatabaseHelper.getWritableDatabase();
Cursor book = db.query("Book", null, null, null, null, null, null);
if(book.moveToFirst()){
do{
String name = book.getString(book.getColumnIndex("name"));
int pages = book.getInt(book.getColumnIndex("pages"));
String author = book.getString(book.getColumnIndex("author"));
double price = book.getDouble(book.getColumnIndex("price"));
Log.e(TAG, "book name is "+name);
Log.e(TAG, "book author is "+author);
Log.e(TAG, "book pages are "+pages );
Log.e(TAG, "book price is "+price );
}while(book.moveToNext());
}
book.close();
}
});
替换数据(使用事务):注释处为手动添加的异常,如果一场存在,状态会回滚,数据不会发生替换,也不会删除,去除异常,可以完成事务,数据被进行替换。
replace.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SQLiteDatabase db = myDatabaseHelper.getWritableDatabase();
db.beginTransaction();
try{
db.delete("Book",null,null);
ContentValues values = new ContentValues();
values.put("name","Android从入门到放弃");
values.put("author","明日科技");
values.put("price",59.8);
values.put("pages",600);
db.insert("Book",null,values);
db.setTransactionSuccessful();
}catch(Exception e){
e.printStackTrace();
}finally {
db.endTransaction();
}
}
});
|