Android数据持久化存储方式
来源:互联网 发布:vb游戏代码大全 编辑:程序博客网 时间:2024/05/21 08:43
Android系统中主要提供了三种方式用于简单的实现数据持久化功能:
1,文件存储
特点:手机自带的内存,只能供当前应用程序访问,其他应用程序访问不了,程序卸载这些数据也会随着消失
- private void save(String inputText ) {
- FileOutputStream fos = null;
- BufferedWriter writer = null;
- try {
- fos = openFileOutput( "data", Context.MODE_PRIVATE);
- writer = new BufferedWriter( new OutputStreamWriter(fos));
- writer.write( inputText);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- try {
- if( writer != null)
- writer.close();
- }catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- //帮助我们返回一个目录
- //context.getCacheDir ()的话就是保存到cache缓存文件夹下面
- File file=new File(context.getFilesDir(), "userinfo.txt");
- FileOutputStream fos= new FileOutputStream( file);
- //zhangsan 123
- fos.write(( username+ "##"+ password).getBytes());
- fos.close();
2、sharedpreference 存储(一般用于保存用户设置偏好);
特点如下:
@1以键值对的形式保存到data/data/应用程序包名/shared_prefs目录的XXX.xml文件中
@2目前支持的数据类型有String int float boolean long
@3不支持自定义的Object
@4通常用来存储App上的用户配置信息.如:是否震动,是否打开背景音乐 小游戏积分 用户账号密码信息
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- SharedPreferences.Editor editor=getSharedPreferences( "data", MODE_PRIVATE).edit();
- editor.putString( "name", "HuaAn");
- editor.putInt( "id", 9527);
- editor.putBoolean( "婚否", false );
- editor.commit();
- }
- });
- restore_button .setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- SharedPreferences pref=getSharedPreferences( "data", MODE_PRIVATE);
- String name= pref.getString( "name", "HuaAn");
- int id= pref.getInt( "id", 9527);
- boolean married= pref.getBoolean( "婚否", false );
- }
- });
- }
3 ,SQLite数据库存储
实现步骤如下:
- public class MyDatabaseHelper extends SQLiteOpenHelper {
- /*
- 补充一下建表的一些类型
- integer ---整型
- real-----浮点类型
- text---文本类型
- blob---二进制类型
- */
- public static final String CREATE_BOOK= "create table book(id integer primary key autoincrement,"
- + "author text"
- + "price real"
- + "pages integer"
- + "name text)";
- private Context mContext ;
- public MyDatabaseHelper(Context context, String name,
- CursorFactory factory, int version) {
- super( context, name, factory, version);
- // TODO Auto-generated constructor stub
- mContext= context;
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- // TODO Auto-generated method stub
- //执行建表语句
- db.execSQL(CREATE_BOOK);
- Toast.makeText(mContext , "数据库创建成功" , Toast.LENGTH_SHORT).show();
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- // TODO Auto-generated method stub
- }
- }
- dbHelper= new MyDatabaseHelper( this, "BookStore.db", null, 1);
- Button createDatabase=(Button) findViewById(R.id.create_database );
- createDatabase.setOnClickListener( new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- dbHelper. getWritableDatabase();
- }
- });
public long insert (String table, String nullColumnHack, ContentValues values)
Convenience method for inserting a row into the database.
Parameters
null
. SQL doesn't allow inserting a completely empty row without naming at least one column name. If your provided values
is empty, no column names are known and an empty row can't be inserted. If not set to null, thenullColumnHack
parameter provides the name of nullable column name to explicitly insert a NULL into in the case where your values
is empty.Returns
- the row ID of the newly inserted row, or -1 if an error occurred
public int update (String table, ContentValues values, String whereClause, String[]whereArgs)
Convenience method for updating rows in the database.
Parameters
Retu rns
- the number of rows affected
public int delete (String table, String whereClause, String[] whereArgs)
Convenience method for deleting rows in the database.
Parameters
Returns
- the number of rows affected if a whereClause is passed in, 0 otherwise. To remove all rows and get a count pass "1" as the whereClause.
查询语句
public Cursor query (String table, String[] columns, String selection, String[]selectionArgs, String groupBy, String having, String orderBy)
Query the given table, returning a Cursor
over the result set.
Parameters
Returns
- A
Cursor
object, which is positioned before the first entry. Note thatCursor
s are not synchronized, see the documentation for more details.
See Also
Cursor
public abstract int getColumnIndex (String columnName)
Returns the zero-based index for the given column name, or -1 if the column doesn't exist. If you expect the column to exist use getColumnIndexOrThrow(String)
instead, which will make the error more clear.
Parameters
Returns
- the zero-based column index for the given column name, or -1 if the column name does not exist.
See Also
getColumnIndexOrThrow(String)
- Android数据持久化存储方式
- Android数据持久化存储方式
- Android-存储方式(持久化数据的方式)
- Android 数据持久化方式(2)SharedPreferences方式存储
- Android 数据持久化技术(即数据存储方式)
- Android 数据持久化技术(即数据存储方式)
- Android 数据持久化方式(1)文件存储
- Android五种数据存储(数据持久化)方式
- Android数据持久化存储
- Android数据持久化存储
- Android--数据存储(数据持久化)
- Android数据持久化数据存储
- Android的数据持久化存储
- android-数据持久化之文件存储
- Android数据持久化存储-SharedPreferences
- Android数据持久化存储-Files对象
- Android的数据持久化存储
- android数据存储持久化操作
- Linux中profile、bashrc、bash_profile之间的区别和联系
- 郝斌的C语言基础 115 合理设计函数
- poj2196
- Spring Boot 学习第一步(搭建初步环境)
- Linux(CentOS)下,下载安装Nginx并配置
- Android数据持久化存储方式
- linux之进程相关知识
- 协处理器
- Spring Boot 学习第二步 配置MySQL数据库+JPA
- MySQL数据库开启远程连接
- SSL P1613 最短路径问题
- springMVC笔记系列(15)——模型数据处理篇 之 @Session注解
- Linux常用命令总结
- C编译器剖析_Github