当前位置:网站首页>Android foundation [Super detailed android storage method analysis (SharedPreferences, SQLite database storage)]
Android foundation [Super detailed android storage method analysis (SharedPreferences, SQLite database storage)]
2022-08-04 06:07:00 【leave the keyboard】
目录
1.Android存储方式
1.1Android共有5medium storage:
SharedPreferences
内部存储(Internal Storage)
外部存储(External Storage)
SQLite数据库存储
网络存储(现阶段主流)
今天我们主要讲解SharedPreferences(文件存储)和SQLite数据库存储.
2.SharedPreferences(文件存储)
2.1简介
文件,相当于properties文件,数据存储都是使用键值对来存储.
2.2写入
a.获取SharedPreferences(①:getSharedPrefernces,②:getPrefernces)
①和②The difference between the acquisition method is the former two parameters(Store file name and mode of operation(int mode)),The latter has only one parameter of operation mode.
b.借助Editor,向SharedPreferenxce中写入数据.
c.写入数据,Only raw basic data can be written(boolean,float,int,long,String),Compound data cannot be written.
d.提交数据.
调用 SharedPreferences.Editor 的 commit() method to submit updates to SharedPreferences 中.
String name="zs";
String pwd="123";
SharedPreferences sp= getSharedPreferences("logininfo",MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.putString("name",name);
edit.putString("pwd",pwd);
edit.commit();
2.3读取
a.获取sharedPrefernces.
b.获取数据.
SharedPreferences sp1= getSharedPreferences("logininfo",MODE_PRIVATE);
String sp_name = sp1.getString("name", "");
String sp_pwd = sp1.getString("pwd", "");
3.SQLite数据库存储
3.1简介
特点:轻量,无需配置.
缺点:本地化.
3.2创建
a.用Class,DBHelper 继承SQLiteOpenHelper.
public class DBHelper extends SQLiteOpenHelper {
}
b.实现两个抽象方法 onCreate() onUpgrade() 创建一个带参的构造方法.
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(@Nullable Context context) {
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
c.在构造方法中 创建数据库.
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(@Nullable Context context) {
super(context, "school.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
d.在onCreate() 方法中,创建表和插入数据.
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(@Nullable Context context) {
super(context, "school.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("create table student(sid integer primary key,sname text,sex text)");
sqLiteDatabase.execSQL("insert into student values(1,'zs','男')");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
边栏推荐
- Redis持久化方式RDB和AOF详解
- 双重指针的使用
- 【CV-Learning】卷积神经网络
- 【go语言入门笔记】12、指针
- 判断字符串是否有子字符串重复出现
- [Introduction to go language] 12. Pointer
- [Deep Learning 21 Days Learning Challenge] 2. Complex sample classification and recognition - convolutional neural network (CNN) clothing image classification
- 剑指 Offer 2022/7/9
- (十)树的基础部分(二)
- 【CV-Learning】语义分割
猜你喜欢
TensorFlow2学习笔记:5、常用激活函数
动手学深度学习__张量
Introduction of linear regression 01 - API use cases
【CV-Learning】卷积神经网络
flink sql left join数据倾斜问题解决
NFT市场以及如何打造一个NFT市场
Install dlib step pit record, error: WARNING: pip is configured with locations that require TLS/SSL
0, deep learning 21 days learning challenge 】 【 set up learning environment
[Deep Learning 21-Day Learning Challenge] 3. Use a self-made dataset - Convolutional Neural Network (CNN) Weather Recognition
剑指 Offer 2022/7/1
随机推荐
Briefly say Q-Q map; stats.probplot (QQ map)
TensorFlow2学习笔记:5、常用激活函数
flink on yarn任务迁移
Lombok的一些使用心得
AIDL communication between two APPs
ValueError: Expected 96 from C header, got 88 from PyObject
Postgresql 快照
Dictionary feature extraction, text feature extraction.
fill_between in Matplotlib; np.argsort() function
MySql--存储引擎以及索引
TensorFlow2 study notes: 7. Optimizer
SQL练习 2022/6/30
超详细MySQL总结
网络大作业心得笔记
Thoroughly understand box plot analysis
简单明了,数据库设计三大范式
判断字符串是否有子字符串重复出现
SQL练习 2022/7/2
flink-sql所有数据类型
【深度学习21天学习挑战赛】2、复杂样本分类识别——卷积神经网络(CNN)服装图像分类