亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android 通過SQLite數(shù)據(jù)庫實(shí)現(xiàn)數(shù)據(jù)存儲管理

 更新時間:2021年11月17日 16:12:30   作者:雨落i  
SQLiteOpenHelper 是Android 提供的一個抽象工具類,負(fù)責(zé)管理數(shù)據(jù)庫的創(chuàng)建、升級工作。本文主要介紹了如何使用SQLite數(shù)據(jù)庫實(shí)現(xiàn)對數(shù)據(jù)進(jìn)行存儲管理,感興趣的可以了解一下

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)一個應(yīng)用可供用戶進(jìn)行數(shù)據(jù)的錄入存儲
(2)能實(shí)現(xiàn)基礎(chǔ)CRUD操作,對數(shù)據(jù)進(jìn)行的刪、查、改等操作
(3)同時要有輸入欄和結(jié)果的展示。

3 設(shè)計(jì)原理

SQLiteOpenHelper 是Android 提供的一個抽象工具類,負(fù)責(zé)管理數(shù)據(jù)庫的創(chuàng)建、升級工作。如果我們想創(chuàng)建數(shù)據(jù)庫,就需要自定義一個類繼承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對主界面進(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>

在這里插入圖片描述

對上述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類,用于初始化一些變量和注冊組件:

在這里插入圖片描述

核心代碼:(這里只展示 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) {  //防止重復(fù)運(yùn)行時重復(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();  //供修改和刪除時使用
        }
    });
}

(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)表不存在時,創(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ù)庫的進(jìn)行增刪查改操作進(jìn)行了定義,使得可以在主程序文件中直接調(diào)用有關(guān)API接口:

在這里插入圖片描述

核心代碼:

//構(gòu)造方法,參數(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);
}

//返回?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ù)存儲管理的文章就介紹到這了,更多相關(guān)SQLite數(shù)據(jù)庫實(shí)現(xiàn)數(shù)據(jù)存儲管理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論