生活随笔
收集整理的這篇文章主要介紹了
Android基础_数据存储
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
Android基礎_數據存儲 Android數據存儲的幾種形式
繼承SQLiteOpenHelper
public?class?NoteSQLiteOpenHelper?extends?SQLiteOpenHelper?{private?static?final?String?TAG?=?"NoteSQLiteOpenHelper";/**?????*?context?上下文?name?數據庫的名稱?cursorfactory?游標工廠?一般設置null?默認游標工廠?version?數據庫的版本?????*?版本號從1開始的?????*??????*?@param?context?????*/public?NoteSQLiteOpenHelper(Context?context)?{super(context,?"note.db",?null,?1);}/**?????*?oncreate?方法?會在數據庫第一創建的時候的是被調用?適合做數據庫表結構的初始化?????*/@Overridepublic?void?onCreate(SQLiteDatabase?db)?{Log.i(TAG,?"oncreate?方法被調用了...");db.execSQL("create?table?account?(id?integer?primary?key?autoincrement?,?name??varchar(20),?money?varchar(20)?)");}@Overridepublic?void?onUpgrade(SQLiteDatabase?db,?int?arg1,?int?arg2)?{Log.i(TAG,"onupdate?方法被調用了?,在這個方法里面做更新數據庫表結構的操作");db.execSQL("DROP?TABLE?IF?EXISTS?"?+?account);this.onCreate(db);}}public?class?NoteDao?{//?因為?任何一個操作都是需要?得到?NoteSQLiteOpenHelper?helper//?把他放在構造方法里面初始化private?NoteSQLiteOpenHelper?helper;public?NoteDao(Context?context)?{helper?=?new?NoteSQLiteOpenHelper(context);}/**?????*?添加一條賬目信息?到數據庫?????*??????*?@param?name?????*????????????花銷的名稱?????*?@param?money?????*????????????金額?????*/public?void?add(String?name,?float?money)?{SQLiteDatabase?db?=?helper.getWritableDatabase();db.execSQL("insert?into?account?(name,money)?values?(?,?)",new?Object[]?{?name,?money?});//?記住?關閉.db.close();}public?void?delete(int?id)?{SQLiteDatabase?db?=?helper.getWritableDatabase();db.execSQL("delete?from?account?where?id=?",?new?Object[]?{?id?});db.close();}public?void?update(int?id,?float?newmoney)?{SQLiteDatabase?db?=?helper.getWritableDatabase();db.execSQL("update?account?set?money?=??where?id=?",?new?Object[]?{newmoney,?id?});db.close();}/**?????*?返回數據庫所有的條目?????*??????*?@return?????*/public?List<NoteBean>?findAll()?{//?得到可讀的數據庫SQLiteDatabase?db?=?helper.getReadableDatabase();List<NoteBean>?noteBeans?=?new?ArrayList<NoteBean>();//?獲取到數據庫查詢的結果游標Cursor?cursor?=?db.rawQuery("select?id,money,name?from?account?",?null);while?(cursor.moveToNext())?{int?id?=?cursor.getInt(cursor.getColumnIndex("id"));String?name?=?cursor.getString(cursor.getColumnIndex("name"));float?money?=?cursor.getFloat(cursor.getColumnIndex("money"));NoteBean?bean?=?new?NoteBean(id,?money,?name);noteBeans.add(bean);bean?=?null;}db.close();return?noteBeans;}/**?????*?模擬一個轉賬的操作.?使用數據庫的事務?????*??????*?@throws?Exception?????*/public?void?testTransaction()?throws?Exception?{//?得到可寫的數據庫SQLiteDatabase?db?=?helper.getWritableDatabase();db.beginTransaction();?//?開始事務try?{db.execSQL("update?account?set?money?=?money?-?5?where?id=??",new?String[]?{?"2"?});db.execSQL("update?account?set?money?=?money?+?5?where?id=??",new?String[]?{?"3"?});db.setTransactionSuccessful();}?catch?(Exception?e)?{//?TODO:?handle?exception}?finally?{db.endTransaction();//關閉事務.db.close();}}}public?class?NoteDao2?{private?NoteSQLiteOpenHelper?helper;public?NoteDao2(Context?context)?{helper?=?new?NoteSQLiteOpenHelper(context);}/**?????*?添加一條賬目信息?到數據庫?????*??????*?@param?name?????*????????????花銷的名稱?????*?@param?money?????*????????????金額?????*??????*?@return?true?插入成功?false?失敗?????*/public?boolean?add(String?name,?float?money)?{SQLiteDatabase?db?=?helper.getWritableDatabase();ContentValues?values?=?new?ContentValues();values.put("name",?name);values.put("money",?money);long?rawid?=?db.insert("account",?null,?values);db.close();if?(rawid?>?0)?{return?true;}?else?{return?false;}}public?boolean?delete(int?id)?{SQLiteDatabase?db?=?helper.getWritableDatabase();int?result?=?db.delete("account",?"id=?",?new?String[]?{?id?+?""?});db.close();if?(result?>?0)?{return?true;}?else?{return?false;}}public?boolean?update(int?id,?float?newmoney)?{SQLiteDatabase?db?=?helper.getWritableDatabase();ContentValues?values?=?new?ContentValues();values.put("id",?id);values.put("money",?newmoney);int?result?=?db.update("account",?values,?"id=?",?new?String[]?{?id+?""?});db.close();if?(result?>?0)?{return?true;}?else?{return?false;}}/**?????*?返回數據庫所有的條目?????*??????*?@return?????*/public?List<NoteBean>?findAll()?{//?得到可讀的數據庫SQLiteDatabase?db?=?helper.getReadableDatabase();List<NoteBean>?noteBeans?=?new?ArrayList<NoteBean>();Cursor?cursor?=?db.query("account",?new?String[]?{?"id",?"name","money"?},?null,?null,?null,?null,?null);while?(cursor.moveToNext())?{int?id?=?cursor.getInt(cursor.getColumnIndex("id"));String?name?=?cursor.getString(cursor.getColumnIndex("name"));float?money?=?cursor.getFloat(cursor.getColumnIndex("money"));NoteBean?bean?=?new?NoteBean(id,?money,?name);noteBeans.add(bean);bean?=?null;}db.close();return?noteBeans;}} 獲取
Internal Storage, Store private data on the device memory.(內部存儲卡) 通過 mContext.getFilesDir()
????????來得到data/data/包名/File目錄
External Storage,Store public data on the shared external storage.(外部存儲卡) TextView?tv?=?(TextView)?findViewById(R.id.tv_sdsize);
File?path?=?Environment.getExternalStorageDirectory();
StatFs?stat?=?new?StatFs(path.getPath());
long?blockSize?=?stat.getBlockSize();
long?availableBlocks?=?stat.getAvailableBlocks();
long?sizeAvailSize?=?blockSize?*?availableBlocks;
String?str?=?Formatter.formatFileSize(this,?sizeAvailSize);
tv.setText(str); SharedPreferences Store private primitive data in key-value pairs. 會在data/data/包名/shared_prefes里面去創建相應的xml文件,根節點是Map,其實內部就是將數據保存到Map集合中,然后將該集合中的數據寫到xml文件中進行保存。 //獲取系統的一個sharedpreference文件?
名字叫 sp
SharedPreferences?sp?=?context.getSharedPreferences("sp",?Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
//創建一個編輯器?可以編輯?sp
Editor?editor?=?sp.edit();
editor.putString("name",?name);
editor.putString("password",?password);
editor.putBoolean("boolean",?false);
editor.putInt("int",?8888);
editor.putFloat("float",3.14159f);
//注意:調用?commit?提交?數據到文件.
editor.commit();
//editor.clear(); SQLiteDatabase
Store structured data in a private database. Android平臺中嵌入了一個關系型數據庫SQLite,和其他數據庫不同的是SQLite存儲數據時不區分類型,例如一個字段聲明為Integer類 型,我們也可以將一個字符串存入,一個字段聲明為布爾型,我們也可以存入浮點數。除非是主鍵被定義為Integer,這時只能存儲64位整數創建數據庫的 表時可以不指定數據類型,例如:
CREATE TABLE person(id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(20)) CREATE TABLE person(id INTEGER PRIMARY KEY AUTOINCREMENT, name) SQLite支持大部分標準SQL語句,增刪改查語句都是通用的,分頁查詢語句和MySQL相同SELECT * FROM person LIMIT 20 OFFSET 10 SELECT * FROM person LIMIT 10,20 select * from reading_history order by _id desc limit 3, 4; delete from test where _id in (select _id from test ORDER BY _id DESC limit 3, 20)
NetworkStore data on the web with your own network server.
清除緩存&清除數據 清除數據會清除/data/data/包名中的所有文件
getCacheDir() ?/data/data/<當前應用程序包名>/cache/,清除緩存的時候會清除該目錄中的內容
轉載于:https://my.oschina.net/wangxnn/blog/348097
總結
以上是生活随笔 為你收集整理的Android基础_数据存储 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。