数据储存
1. 存储在DATA(/data/data/包名/files)目录下
2. 储存在sdcard中(/mnt/sdcard/)
3. 以XML格式保存数据在SharedPrefences中
4. 保存数据在SQLite数据库中
常用SQL语句
create table t_user(_id intrger primary key,c_name varchar(20),c_age integer,c_phone carchar(20));
insert into t_user(c_name,c_age,c_phone)values('张三',12,'1223078');
delete from t_user where c_name = '王二';
update t_user set c_age = 44 where c_name= '王二';
select * from t_user;
select * from blacklist limit 20 offset 1
01. 存储在DATA(/data/data/包名/files)目录下
- 创建文件:FileOutputStream fos = openFileOutput(“info.txt”, MODE_PRIVATE);
- 打开文件:FileInputStream fis = openFileInput(“info.txt”);
- 获得文件路径
InputStream in = getAssets().open(“address.zip”);
File outFile = new File(context.getFilesDir(), “address.db”); - 删除文件:boolean deleteFile = deleteFile(“info.txt”);
- 特点
- 该文件创建为私有的,数据是安全的
- 随着应用的卸载而删除
- 适合保存数据量比较小的数据
02. 储存在sdcard中(/mnt/sdcard/)
- 获取SD卡的根目录 (用普通的IO实现文件的读写)
- 需要动态的获取SD的根目录,不同的手机目录不同
File file= Environment.getExternalStorageDirectory();
- 获取SD卡挂载状态
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){}
- 获取在SD卡的空间大小
- FIle方式:
- long FreeSpace = file.getFreeSpace();
- long totalSpace = file.getTotalSpace();
2.StatFS方式:
- StatFs statFs = new StatFs(file.getAbsolutePath());
- int blockCount = statFs.getBlockCount();//获取总块数
- int blockSize = statFs.getBlockSize();//获取每块的大小
- int availableBlocks = statFs.getAvailableBlocks();//获取可用块数
- long totalSize = blockCount * blockSize; long useableSize = availableBlocks *blockSize;
- FIle方式:
数据的格式化:
String total = Formatter.formatFileSize(this, totalSize);
4.特点:- 操作SD卡需要添加写权限(WRITE_EXTERNAL_STORAGE), 读权限(READ_EXTERNAL_STORAGE)暂不考虑
- 可以储存大文件.
通过Environment获取ROM和SD卡的存储状态.
private void initRomData() {
// 初始化内部存储状态
File romFile = Environment.getDataDirectory();
// 初始化SD卡存储状态
//File sdFile = Environment.getExternalStorageDirectory();
long romTotal = romFile.getTotalSpace();
long romFree = romFile.getFreeSpace();
long romUsed = romTotal - romFree;
//file.getUsableSpace(),获取已用的空间,但系统计算的时候是不包括缓存的.
ssv_rom.setUsedText(Formatter.formatFileSize(this, romUsed));
ssv_rom.setFreeText(Formatter.formatFileSize(this, romFree));
ssv_rom.setPBProgress((int) (romUsed * 100f / romTotal));
}
03. 以XML格式保存数据在SharedPrefences中
- 获取sp对象 (底层由map集合实现)
SharedPreferences sp = getSharedPreferences("info", MODE_PRIVATE);
- 保存数据
sp.edit().putString("username", username).putString("pwd", pwd).commit();
- 删除数据
sp.edit().remove("name").clear().commit();
- 读取数据
String username = sp.getString("username", "");
04. 保存数据在SQLite数据库中
1. SQL语句增删改查(CRUD)
create table t_user (_id integer primary key,c_name varchar(20),c_age integer;)
insert into t_user (c_name,c_age) values ('xxx',yy);
delete from t_user where c_age>28 and c_age<31;
update t_user set c_name = 'zzz' where c_id = 23;
select * from t_user; select c_name,c_age from t_user where c_id <10 order by c_age desc limit 10;
//分页查询
select * from blacklist limit 20 offset 1
alter table t_able add c_money float;
2. 数据库的CRUD
1. 定义一个继承SQLiteOpenHelper的myOpenHelper 类(需要在Oncreate中创建一张表)
2. 创建数据库
MyOpenHelper myOpenHelper = newMyOpenHelper(getApplicationContext(),1);//此步不会创建数据库
SQLiteDatabase database= mySqliteOpenHelper.getReadableDatabase();
SQLiteDatabase database= mySqliteOpenHelper.getWritableDatabase();
//普通情况下没有任何区别,都可读可写,只有在磁盘满的情况下,前者只读,后者报异常.
insert
//sql语句
String sql = "insert into t_user(c_name)values(?)"; database.execSQL(sql,new String[]{"张三"});
//androidAPI
ContentValues values = new ContentValues(); values.put("c_name", "zhangsan"); long insert = database.insert("t_user", null, values);//返回插入的行数
delete
//sql语句
String sql = "delete from t_user where c_name=?"; database.execSQL(sql,new String[]{"张三"});
//androidAPI
int delete = database.delete("t_user", "c_name=?",newString[]{"zhangsan"});//返回值是删除成功的行数
update
//sql语句
String sql = "update t_user set c_name=? where c_name=?"; database.execSQL(sql,new String[]{"lisi","zhangsan"});
//androidAPI
ContentValues values = new ContentValues(); values.put("c_name", "lisi"); int update = database.update("t_user", values, "c_name=?", new String[]{"zhangsan"});
query
//sql语句
String sql = "select c_name from t_user where c_name=?";
Cursor cursor = database.rawQuery(sql, new String[]{"lisi"});
while(cursor.moveToNext()){
String name = cursor.getString(0);
}
cursor.close();//需要关闭游标,避免内存溢出
//androidAPI
Cursor cursor = database.query("t_user", new String[]{"c_name"}, "c_name=?", new String[]{"lisi"}, null, null, "c_name desc");
while(cursor.moveToNext()){
int columnIndex = cursor.getColumnIndex("c_name");//根据字段名称获取字段在结果集中的索引
String name = cursor.getString(columnIndex);
// String name = cursor.getString(0);//这样子也是可以的
}
cursor.close();
3.数据库的升级
//通过SQLiteOpenHelper的构造函数中的Version变量来实现的
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("alter table info add phone varchar(20)");
}
4.数据库命令
1. adb shell 先进入Linux命令行
2. cd /data/data/包名/databases
3. sqlite3 user.db
4. select * from t_user;