Android 通過SQLite數(shù)據(jù)庫實現(xiàn)數(shù)據(jù)存儲管理
0 實驗環(huán)境
在Android Studio中進行有關代碼的編寫和界面效果展示。
SQLite數(shù)據(jù)庫的圖形化工具SQLiteStudio
下載網(wǎng)址:SQLiteStudio官網(wǎng)
1 界面展示
2 功能說明
(1)需實現(xiàn)一個應用可供用戶進行數(shù)據(jù)的錄入存儲
(2)能實現(xiàn)基礎CRUD操作,對數(shù)據(jù)進行的刪、查、改等操作
(3)同時要有輸入欄和結果的展示。
3 設計原理
SQLiteOpenHelper 是Android 提供的一個抽象工具類,負責管理數(shù)據(jù)庫的創(chuàng)建、升級工作。如果我們想創(chuàng)建數(shù)據(jù)庫,就需要自定義一個類繼承SQLiteOpenHelper,然后覆寫其中的抽象方法。
其中DbHelper類繼承了SQLiteOpenHelper 類。在onCreate()方法中通過執(zhí)行sql 語句實現(xiàn)表的創(chuàng)建。MyDAO類定義操作數(shù)據(jù)庫的增刪改查方法,insert(),delete(),update(),select()
4 核心代碼
4.1 UI設計
activity_main.xml對主界面進行設計,需要有輸入欄、操作按鈕、結果顯示,其中結果顯示使用listView組件進行展示。
部分代碼:
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:addStatesFromChildren="true" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="姓名" android:fontFamily="@font/huawencaiyun" android:textColor="@color/blue" android:textSize="25sp"/> <EditText android:id="@+id/et_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:singleLine="true" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:addStatesFromChildren="true" android:gravity="center" > <Button android:id="@+id/bt_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="添加" /> </LinearLayout> <ListView android:gravity="center" android:id="@+id/listView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5dip" > </ListView>
對上述listView組件的item元素設計:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@+id/tv_id" android:textSize="25sp" android:layout_width="50dp" android:layout_height="wrap_content"/> </LinearLayout>
4.2 編寫有關Java類
(1)MainActivity類,用于初始化一些變量和注冊組件:
核心代碼:(這里只展示 onCreate和displayRecords兩個方法)
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myDAO = new MyDAO(this); //創(chuàng)建數(shù)據(jù)庫訪問對象 if(myDAO.getRecordsNumber()==0) { //防止重復運行時重復插入記錄 myDAO.insertInfo("王家偉", 20); //插入記錄 myDAO.insertInfo("結衣", 19); //插入記錄 } initView(); initEvent(); displayRecords(); //顯示記錄 } public void displayRecords(){ //顯示記錄方法定義 listView = (ListView)findViewById(R.id.listView); listData = new ArrayList<Map<String,Object>>(); Cursor cursor = myDAO.allQuery(); while (cursor.moveToNext()){ int id=cursor.getInt(0); //獲取字段值 String name=cursor.getString(1); //int age=cursor.getInt(2); @SuppressLint("Range") int age=cursor.getInt(cursor.getColumnIndex("age"));//推薦此種方式 listItem=new HashMap<String,Object>(); //必須在循環(huán)體里新建 listItem.put("_id", id); //第1參數(shù)為鍵名,第2參數(shù)為鍵值 listItem.put("name", name); listItem.put("age", age); listData.add(listItem); //添加一條記錄 } listAdapter = new SimpleAdapter(this, listData, R.layout.list_item, //自行創(chuàng)建的列表項布局 new String[]{"_id","name","age"}, new int[]{R.id.tv_id,R.id.tv_name,R.id.tv_age}); listView.setAdapter(listAdapter); //應用適配器 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { //列表項監(jiān)聽 @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Map<String,Object> rec= (Map<String, Object>) listAdapter.getItem(position); //從適配器取記錄 et_name.setText(rec.get("name").toString()); //刷新文本框 et_age.setText(rec.get("age").toString()); Log.i("ly",rec.get("_id").toString()); selId=rec.get("_id").toString(); //供修改和刪除時使用 } }); }
(2)DbHelper類是SQLite數(shù)據(jù)庫打開助手類,用來創(chuàng)建和打開數(shù)據(jù)庫,并創(chuàng)建數(shù)據(jù)表:
核心代碼:
public class DbHelper extends SQLiteOpenHelper { public static final String TB_NAME = "friends"; //表名 //構造方法:第1參數(shù)為上下文,第2參數(shù)庫庫名,第3參數(shù)為游標工廠,第4參數(shù)為版本 public DbHelper(Context context, String dbname, SQLiteDatabase.CursorFactory factory, int version) { super(context, dbname, factory, version); //創(chuàng)建或打開數(shù)據(jù)庫 } @Override public void onCreate(SQLiteDatabase db) { //當表不存在時,創(chuàng)建表;第一字段為自增長類型 db.execSQL("CREATE TABLE IF NOT EXISTS " + TB_NAME + "( _id integer primary key autoincrement," + "name varchar," + "age integer"+ ")"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // 執(zhí)行SQL命令 db.execSQL("DROP TABLE IF EXISTS " + TB_NAME); onCreate(db); } }
(3)MyDAO類中對數(shù)據(jù)庫的進行增刪查改操作進行了定義,使得可以在主程序文件中直接調用有關API接口:
核心代碼:
//構造方法,參數(shù)為上下文對象 public MyDAO(Context context) { //第1參數(shù)為上下文,第2參數(shù)為數(shù)據(jù)庫名 dbHelper = new DbHelper(context, "test.db", null, 1); } //查詢所有記錄 public Cursor allQuery() { myDb = dbHelper.getReadableDatabase(); return myDb.rawQuery("select * from friends", null); } //返回數(shù)據(jù)表記錄數(shù) public int getRecordsNumber() { myDb = dbHelper.getReadableDatabase(); Cursor cursor = myDb.rawQuery("select * from friends", null); return cursor.getCount(); } //插入記錄 public void insertInfo(String name, int age) { myDb = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("name", name); values.put("age", age); long rowid = myDb.insert(DbHelper.TB_NAME, null, values); if (rowid == -1) Log.i("myDbDemo", "數(shù)據(jù)插入失??!"); else Log.i("myDbDemo", "數(shù)據(jù)插入成功!" + rowid); } //刪除記錄 public void deleteInfo(String selId) { String where = "_id=" + selId; int i = myDb.delete(DbHelper.TB_NAME, where, null); if (i > 0) Log.i("myDbDemo", "數(shù)據(jù)刪除成功!"); else Log.i("myDbDemo", "數(shù)據(jù)未刪除!"); } //修改記錄 public void updateInfo(String name, int age, String selId) { //方法中的第三參數(shù)用于修改選定的記錄 ContentValues values = new ContentValues(); values.put("name", name); values.put("age", age); String where = "_id=" + selId; int i = myDb.update(DbHelper.TB_NAME, values, where, null); //上面幾行代碼的功能可以用下面的一行代碼實現(xiàn) //myDb.execSQL("update friends set name = ? ,age = ? where _id = ?",new Object[]{name,age,selId}); if (i > 0) Log.i("myDbDemo", "數(shù)據(jù)更新成功!"); else Log.i("myDbDemo", "數(shù)據(jù)未更新!"); }
(4)使用SQLite數(shù)據(jù)庫的圖形化工具SQLiteStudio,來查看在應用中創(chuàng)建的數(shù)據(jù)文件里的表數(shù)據(jù):
注意:創(chuàng)建的數(shù)據(jù)庫位于:/data/data/包名/databases/目錄中
最后使用SQLite數(shù)據(jù)庫的圖形化工具SQLiteStudio,將保存的db文件導入SQLiteStudio中,來查看在應用中創(chuàng)建的數(shù)據(jù)文件里的表數(shù)據(jù)
數(shù)據(jù)表的結構:
數(shù)據(jù)表的數(shù)據(jù):
5 代碼倉庫
具體代碼已上傳至gitee代碼倉庫
6 總結
學習了如何創(chuàng)建SQLite數(shù)據(jù)庫和基礎CRUD操作,最后用圖形化工具SQLiteStudio查看生成的數(shù)據(jù)庫中的數(shù)據(jù)表!
到此這篇關于Android 通過SQLite數(shù)據(jù)庫實現(xiàn)數(shù)據(jù)存儲管理的文章就介紹到這了,更多相關SQLite數(shù)據(jù)庫實現(xiàn)數(shù)據(jù)存儲管理內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- android?studio數(shù)據(jù)存儲建立SQLite數(shù)據(jù)庫實現(xiàn)增刪查改
- Android四種數(shù)據(jù)存儲的應用方式
- Android基礎教程數(shù)據(jù)存儲之文件存儲
- Android SharedPreferences實現(xiàn)數(shù)據(jù)存儲功能
- android使用SharedPreferences進行數(shù)據(jù)存儲
- Android開發(fā)教程之ContentProvider數(shù)據(jù)存儲
- 詳解Android的網(wǎng)絡數(shù)據(jù)存儲
- 5種Android數(shù)據(jù)存儲方式匯總
- 詳解Android數(shù)據(jù)存儲之SQLCipher數(shù)據(jù)庫加密
- Android 單例模式實現(xiàn)可復用數(shù)據(jù)存儲的詳細過程
相關文章
Android Fragment(動態(tài),靜態(tài))碎片詳解及總結
這篇文章主要介紹了Android Fragment詳解及總結的相關資料,這里對Android Fragment 動態(tài),靜態(tài)碎片進行了整理總結,需要的朋友可以參考下2016-12-12Android中Xposed框架篇---修改系統(tǒng)位置信息實現(xiàn)自身隱藏功能實例
本篇文章介紹了Android中Xposed框架的使用,詳細的介紹了修改系統(tǒng)位置信息實現(xiàn)自身隱藏功能實例,有需要的朋友可以了解一下。2016-11-11Android SQLite操作之大數(shù)據(jù)處理與同時讀寫方法
這篇文章主要介紹了Android SQLite操作之大數(shù)據(jù)處理與同時讀寫方法,實例分析了Android操作SQLite時基于事務的數(shù)據(jù)緩存與批量插入技巧,以及同時讀寫的相關實現(xiàn)方法與注意事項,需要的朋友可以參考下2016-07-07