Android 通過SQLite數(shù)據(jù)庫實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)管理
0 實(shí)驗(yàn)環(huán)境
在Android Studio中進(jìn)行有關(guān)代碼的編寫和界面效果展示。
SQLite數(shù)據(jù)庫的圖形化工具SQLiteStudio
下載網(wǎng)址:SQLiteStudio官網(wǎng)

1 界面展示


2 功能說明
(1)需實(shí)現(xiàn)一個(gè)應(yīng)用可供用戶進(jìn)行數(shù)據(jù)的錄入存儲(chǔ)
(2)能實(shí)現(xiàn)基礎(chǔ)CRUD操作,對(duì)數(shù)據(jù)進(jìn)行的刪、查、改等操作
(3)同時(shí)要有輸入欄和結(jié)果的展示。
3 設(shè)計(jì)原理
SQLiteOpenHelper 是Android 提供的一個(gè)抽象工具類,負(fù)責(zé)管理數(shù)據(jù)庫的創(chuàng)建、升級(jí)工作。如果我們想創(chuàng)建數(shù)據(jù)庫,就需要自定義一個(gè)類繼承SQLiteOpenHelper,然后覆寫其中的抽象方法。
其中DbHelper類繼承了SQLiteOpenHelper 類。在onCreate()方法中通過執(zhí)行sql 語句實(shí)現(xiàn)表的創(chuàng)建。MyDAO類定義操作數(shù)據(jù)庫的增刪改查方法,insert(),delete(),update(),select()
4 核心代碼
4.1 UI設(shè)計(jì)

activity_main.xml對(duì)主界面進(jìn)行設(shè)計(jì),需要有輸入欄、操作按鈕、結(jié)果顯示,其中結(jié)果顯示使用listView組件進(jìn)行展示。
部分代碼:
<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>

對(duì)上述listView組件的item元素設(shè)計(jì):
<?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 編寫有關(guān)Java類

(1)MainActivity類,用于初始化一些變量和注冊(cè)組件:

核心代碼:(這里只展示 onCreate和displayRecords兩個(gè)方法)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDAO = new MyDAO(this); //創(chuàng)建數(shù)據(jù)庫訪問對(duì)象
if(myDAO.getRecordsNumber()==0) { //防止重復(fù)運(yùn)行時(shí)重復(fù)插入記錄
myDAO.insertInfo("王家偉", 20); //插入記錄
myDAO.insertInfo("結(jié)衣", 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)建的列表項(xiàng)布局
new String[]{"_id","name","age"},
new int[]{R.id.tv_id,R.id.tv_name,R.id.tv_age});
listView.setAdapter(listAdapter); //應(yīng)用適配器
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { //列表項(xiàng)監(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(); //供修改和刪除時(shí)使用
}
});
}
(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"; //表名
//構(gòu)造方法:第1參數(shù)為上下文,第2參數(shù)庫庫名,第3參數(shù)為游標(biāo)工廠,第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) {
//當(dāng)表不存在時(shí),創(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類中對(duì)數(shù)據(jù)庫的進(jìn)行增刪查改操作進(jìn)行了定義,使得可以在主程序文件中直接調(diào)用有關(guān)API接口:

核心代碼:
//構(gòu)造方法,參數(shù)為上下文對(duì)象
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);
}
//返回?cái)?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);
//上面幾行代碼的功能可以用下面的一行代碼實(shí)現(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,來查看在應(yīng)用中創(chuàng)建的數(shù)據(jù)文件里的表數(shù)據(jù):
注意:創(chuàng)建的數(shù)據(jù)庫位于:/data/data/包名/databases/目錄中

最后使用SQLite數(shù)據(jù)庫的圖形化工具SQLiteStudio,將保存的db文件導(dǎo)入SQLiteStudio中,來查看在應(yīng)用中創(chuàng)建的數(shù)據(jù)文件里的表數(shù)據(jù)
數(shù)據(jù)表的結(jié)構(gòu):

數(shù)據(jù)表的數(shù)據(jù):

5 代碼倉庫
具體代碼已上傳至gitee代碼倉庫
6 總結(jié)
學(xué)習(xí)了如何創(chuàng)建SQLite數(shù)據(jù)庫和基礎(chǔ)CRUD操作,最后用圖形化工具SQLiteStudio查看生成的數(shù)據(jù)庫中的數(shù)據(jù)表!
到此這篇關(guān)于Android 通過SQLite數(shù)據(jù)庫實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)管理的文章就介紹到這了,更多相關(guān)SQLite數(shù)據(jù)庫實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)管理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- android?studio數(shù)據(jù)存儲(chǔ)建立SQLite數(shù)據(jù)庫實(shí)現(xiàn)增刪查改
- Android四種數(shù)據(jù)存儲(chǔ)的應(yīng)用方式
- Android基礎(chǔ)教程數(shù)據(jù)存儲(chǔ)之文件存儲(chǔ)
- Android SharedPreferences實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)功能
- android使用SharedPreferences進(jìn)行數(shù)據(jù)存儲(chǔ)
- Android開發(fā)教程之ContentProvider數(shù)據(jù)存儲(chǔ)
- 詳解Android的網(wǎng)絡(luò)數(shù)據(jù)存儲(chǔ)
- 5種Android數(shù)據(jù)存儲(chǔ)方式匯總
- 詳解Android數(shù)據(jù)存儲(chǔ)之SQLCipher數(shù)據(jù)庫加密
- Android 單例模式實(shí)現(xiàn)可復(fù)用數(shù)據(jù)存儲(chǔ)的詳細(xì)過程
相關(guān)文章
Android啟動(dòng)頁用戶相關(guān)政策彈框的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android啟動(dòng)頁用戶相關(guān)政策彈框的實(shí)現(xiàn)方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
Android studio導(dǎo)出APP測試包和構(gòu)建正式簽名包
大家好,本篇文章主要講的是Android studio導(dǎo)出APP測試包和構(gòu)建正式簽名包,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2021-12-12
Android實(shí)現(xiàn)socket通信統(tǒng)一接口的方法
這篇文章主要介紹了Android實(shí)現(xiàn)socket通信統(tǒng)一接口?,實(shí)現(xiàn)了統(tǒng)一接口之后確實(shí)可以使后續(xù)修改實(shí)現(xiàn)更加方便,程序結(jié)構(gòu)也更加工程化,需要的朋友可以參考下2021-12-12
Android實(shí)現(xiàn)多進(jìn)程并發(fā)控制的兩種方案
當(dāng)一個(gè)App中存在多個(gè)進(jìn)程時(shí)例如存在?主進(jìn)程,輔進(jìn)程兩個(gè)進(jìn)程,兩個(gè)進(jìn)程都會(huì)去向A文件中寫入數(shù)據(jù),但是我們業(yè)務(wù)中希望每次僅允許有一個(gè)進(jìn)程向A文件寫入內(nèi)容,所以本文給大家介紹了Android實(shí)現(xiàn)多進(jìn)程并發(fā)控制的兩種方案,需要的朋友可以參考下2025-04-04
Android編程實(shí)現(xiàn)XML解析與保存的三種方法詳解
這篇文章主要介紹了Android編程實(shí)現(xiàn)XML解析與保存的三種方法,結(jié)合實(shí)例形式詳細(xì)分析了Android實(shí)現(xiàn)xml解析的SAX、DOM、PULL三種方法的相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
一文搞懂Android RecyclerView點(diǎn)擊展開、折疊效果的實(shí)現(xiàn)代碼
雖然在日常開發(fā)中已經(jīng)多次接觸過RecycleView,但也只是用到其最基本的功能,并沒有深入研究其他內(nèi)容。接下來將抽出時(shí)間去了解RecycleView的相關(guān)內(nèi)容,這篇文章主要是介紹Android RecyclerView點(diǎn)擊展開、折疊效果的實(shí)現(xiàn)方式,一起看看吧2021-06-06
Android中ConstraintLayout約束布局的最全詳細(xì)解析
ConstraintLayout是Google在Google?I/O?2016大會(huì)上發(fā)布的一種新的布局容器(ViewGroup),它支持以靈活的方式來放置子控件和調(diào)整子控件的大小,下面這篇文章主要給大家介紹了關(guān)于Android中ConstraintLayout約束布局詳細(xì)解析的相關(guān)資料,需要的朋友可以參考下2022-08-08
Android 中WebView 截圖的實(shí)現(xiàn)方式
這篇文章主要介紹了Android 中WebView 截圖的實(shí)現(xiàn)方式,WebView 作為一種特殊的控件,自然不能像其他系統(tǒng) View 或者截屏的方式來獲取截圖。本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2017-12-12

