文件存储
写数据
String data = "Data ti save";
FileOutputStream out =null;
BufferedWriter writer =null;
try{
out = openFileOutput("data",Context.MODE_PRIVATE)//MODE_APPEND有同名文件时追加,PRIVATE是默认,有同名文件时覆盖
writer = new BufferedWriter(new OutpurStreamWriter(out));
writer.writer(data);
}
catch(IOexception e)
{
e.printStackTrace();
}
finally
{
try{
if(writer!=null)
{
writer.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
- 在右侧的Device File Explorer 查看存储的文件
- 存放路径/data/data/com..../files/
读数据
P201
SharedPreferences存储
- 存放路径 /data/data/com.../shared_prefs
一、Context类中的getSharedPreferences()方法
getSharedPreferences("file_name","MODE_PRIVATE")//第二个参数只有一种
二、Activity类中的getPreferences(MODE_PRIVATE)//文件名默认我活动类名
三、PreferenceManager类中的getDefaultSharedPreferences()方法
具体方法
写
SharePreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
editor.putString("name","Tom");
editor.putInt("age",28);
editor.apply();//提交
读
ShareOreferences pref = getSharedPreferences("data",MODE_PRIVATE);
String name=pref.getString("name","")//若找不到值 就用方法传进来的值代替
SQLite数据库
-
存储位置 /data/data/com.example.administrator.myapplication/databases
-
SQLiteOpenHelper 两个实例方法
-
getReadableDatabase()//都可以对可读写的数据库进行读写
-
getWriteableDatabase()//不可写入的时候 使用此方法报错
构造方法
SQLiteOpwnHelper(Context,"database_name",null,"1"//database_version);//当最后一个参数大于上一个版本时 会执行onUpgrade方法
package com.example.administrator.myapplication;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
public class MyDatabaseHelper extends SQLiteOpenHelper {
public static final String CREATE_BOOK=
"create table Book(" +
"id integer primary key autoincrement," +
"author text," +
"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 sqLiteDatabase) {
sqLiteDatabase.execSQL(CREATE_BOOK);//执行建表语句
Toast.makeText(mContext,"yes",Toast.LENGTH_LONG).show();
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
db.execSQL("drop table if exists Book");
db.execSQL("drop table if exists Category");
onCreate(db);//升级数据库
}
}
更新
SQLiteDatabase db = SQLiteOpenHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("key","value");
db.insert("table_name",null,values);//插入
db.update("table_name",values,"name=?",new String[] {"The Da "}) //?是个占位符,The Da 会替换? 意思就是更新 名为 The Da 的内容
db.delete("table_name","pages>?",new String[] {"500"})//删除大于500行的数据
values,clear();
adb shell
- sqlite3 databasename.db
- .table 查看有哪些表
- .schema 建表语句
查询
使用query()方法七个参数
SQLiteDatabase db = SQLiteOpenHelper.getWritableDatabase();
Cursor cursor = db.query("Bool",null,null,null,null,null,null,null);
if(cursor.moveToFirst())
{
do{
String name=cursor.getString(cursor.getColumnIndex("name"));
}while(cursor.moveToNext());
}
cursor.close();
使用sql方式
db.execSQL("insert into Book (name,author,pages,price) values(?,?,?,?),new String[] {"sfs","sfsf",“23.5”,”454"}");
db.rawQuery("select * from Book",null);