XML–SharePreferrence
保存位置为 :/data/data/ 应用包名 /shared_prefs/xml 文件
存储:
//(xml 文件名 , 操作模式 )
SharedPreferences sh = getSharedPreferences(XML_NAME, Context.MODE_PRIVATE);
// 存储内容需要有 Editor
SharedPreferences.Editor ed = sh.edit();
ed.putString("name",etName.getText().toString());
ed.putString("psw",etPsw.getText().toString());
// 提交
ed.commit();
读取:
//(xml 文件名 , 操作模式 )
SharedPreferences sh = getSharedPreferences(XML_NAME, Context.MODE_PRIVATE);
String name = sh.getString("name","");
String psw = sh.getString("psw","") ;
File
存储位置 /data/data/ 应用包名 /files/ 文件名
保存:
// 保存内容
try {
// 打开输出流
FileOutputStream out = openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
// 写内容到文件中
writer.write(etName.getText().toString());
writer.flush();
writer.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
读取:
try {
// 打开输入流
FileInputStream in = openFileInput(FILE_NAME);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
// 读取内容
String name = br.readLine();
br.close();
in.close();
etName.setText(name);
} catch (Exception e) {
e.printStackTrace();
}
Sqlite( 轻量级数据库 )
Sqlite 基本数据类型: null( 空类型 ) , integer( 整数 ) , text( 字符串 ) , real( 浮点数 ),blob( 大二进制数据 )
建表
create table article (_id integer primary key
autoincrement,
title text not null,
content text not null,
time text)
添加数据
insert into article (title,content,time)
values
('Hello','Android sqlite','123456789899');
省略字段插入
insert into article values (2,'Abc123','qweqweqweqwe','98985656565');
更新数据
update article set title=' 桃花源记 ',content=' 桃花源记内容 ' where _id=2
删除
delete from article where _id=1
查询
drop table if exists article
Android 中使用 Sqlite
1 、需要定义 SqliteOpenHelper 类操作数据库
public class DbOpenUtil extends SQLiteOpenHelper {
private static final String DB_NAME = "dataSave.db";
private static final int VERSION = 2;
public DbOpenUtil(Context context) {
//检测/data/data/应用包名/database/dataSave.db是否存在
//不存在就创建,创建成功触发onCreate
//如果已存在,检测当前版本跟数据库版本号是否一致,如果当前版本高
super(context, DB_NAME, null, VERSION);
}
// 数据库创建成功时触发
@Override
public void onCreate(SQLiteDatabase db) {
//建一个文章表
String sql = "create table article (_id integer primary key autoincrement,title text not null,content text not null,time text)";
//执行sql语句
db.execSQL(sql);
}
//数据库更新时触发
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.e("m_tag","onUpgrade:old:"+oldVersion+" new:"+newVersion);
}
}
使用:
mDbOpenUtil = new DbOpenUtil(this);
打开数据库
// 打开一个可写的数据库
SQLiteDatabase db = mDbOpenUtil.getWritableDatabase();
// 插入数据
String sql = "insert into article (title,content,time) values ('Hello','Android sqlite','"+System.currentTimeMillis()+"');";
db.execSQL(sql);
插入数据:
//插入到数据表中的键值对(插入的内容)对象
ContentValues values = new ContentValues();
values.put("title", title);
values.put("content", content);
values.put("time", String.valueOf(System.currentTimeMillis()));
long rows = db.insert("article", null, values);
if (rows > 0) {
Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "添加失败", Toast.LENGTH_SHORT).show();
}
查询数据:
// 查询字段
String[] items = {"_id", "title", "content", "time"};
//select _id,title,content,time from article
Cursor c = db.query("article", items, null, null, null, null, null);
// 获取总条数
//int count = c.getCount();
StringBuilder sb = new StringBuilder();
while(c.moveToNext()){
// 读数据
int id = c.getInt(0);
String t1 = c.getString(1);
String c1 = c.getString(2);
String time = c.getString(3);
// 处理查询到的数据
sb.append(id).append(" ").append(t1).append(" ").append(c1).append(" ").append(time).append("\n");
}
// 关闭查询
c.close();
tvResult.setText(sb.toString());
如果查询结果中不指定查询字段,可以使用字段名获取字段下标
Cursor c = db.query("article", null, null, null, null, null, null);
StringBuilder sb = new StringBuilder();
while(c.moveToNext()){
// 读数据
//c.getColumnIndex("_id") 可以根据字段名获取下标 ( 字段必须在查询结果中 )
//c.getColumnIndexOrThrow("_id") 可以根据字段名获取下标 ( 字段可以不在查询结果中 )
int id = c.getInt(c.getColumnIndexOrThrow("_id"));
String t1 = c.getString(c.getColumnIndexOrThrow("title"));
String c1 = c.getString(c.getColumnIndexOrThrow("content"));
String time = c.getString(c.getColumnIndexOrThrow("time"));
// 处理查询到的数据
sb.append(id).append(" ").append(t1).append(" ").append(c1).append(" ").append(time).append("\n");
}
关于条件
// 可以 ( 第三个参数 ) 使用问号预设条件,然后再 ( 第四个参数 ) 通过 String 数组一一填充条件值
Cursor c = db.query("article", null, "_id>? and title=?", new String[]{"2","abc"}, null, null, null);
更新数据:
// 插入到数据表中的键值对 ( 插入的内容 ) 对象
ContentValues values1 = new ContentValues();
values1.put("title", title1);
values1.put("content", content1);
values1.put("time", String.valueOf(System.currentTimeMillis()));
String idStr = etId.getText().toString();
// 更新数据
int r = db.update("article",values1,"_id=?",new String[]{idStr});
if(r>0){
// 成功
}
删除数据:
String idStr1 = etId.getText().toString();
int r1 = db.delete("article","_id=?",new String[]{idStr1});
if(r1>0){
Toast.makeText(this, " 删除成功 ", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, " 删除失败 ", Toast.LENGTH_SHORT).show();
}