最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android數(shù)據(jù)庫增刪改查實(shí)戰(zhàn)案例

 更新時(shí)間:2023年04月19日 09:27:10   作者:微笑伴你而行  
我們在編程中經(jīng)常會遇到數(shù)據(jù)庫的操作,這篇文章主要給大家介紹了關(guān)于Android數(shù)據(jù)庫增刪改查的相關(guān)資料,文中通過圖文以及實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

一、案例演示

請?zhí)砑訄D片描述

二、實(shí)現(xiàn)步驟

1、activity_main.xml

頁面布局

代碼

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/tv_username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:layout_marginTop="10dp"
        android:text="用戶名:"/>

    <EditText
        android:id="@+id/et_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_toRightOf="@id/tv_username"
        android:minLines="2" />


    <EditText
        android:id="@+id/et_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/et_username"
        android:layout_alignLeft="@id/et_username"
        android:layout_marginTop="10dp"
        android:inputType="textPassword"
        android:minLines="2"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/tv_choiceB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/et_password"
        android:textSize="30sp"
        android:text="密    碼:"/>

    <EditText
        android:id="@+id/et_age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/et_password"
        android:layout_alignLeft="@id/et_password"
        android:layout_marginTop="10dp"
        android:minLines="2" />

    <TextView
        android:id="@+id/tv_age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/et_age"
        android:textSize="30sp"
        android:text="年    齡:"/>

    <Button
        android:id="@+id/bt_save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_age"
        android:layout_alignTop="@id/bt_query"
        android:text="保存"
        android:textSize="25sp" />

    <Button
        android:id="@+id/bt_update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="修改"
        android:layout_toRightOf="@id/bt_query"
        android:layout_below="@id/et_age"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:textSize="25sp"
        />

    <Button
        android:id="@+id/bt_delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="刪除"
        android:layout_toRightOf="@id/bt_update"
        android:layout_below="@id/et_age"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:textSize="25sp"
        />

    <Button
        android:id="@+id/bt_query"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/et_age"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_toRightOf="@id/bt_save"
        android:text="查詢"
        android:textSize="25sp" />

    <TextView
        android:id="@+id/tv_show"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/bt_save"
        android:textSize="25dp" />

</RelativeLayout>

2、MainActivity.java

通過點(diǎn)擊不同的按鈕,進(jìn)行不同的增刪改查操作

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private SQLiteOpenHelper helper;
    private UserDao userDao;
    private User user;
    private EditText et_username,et_password,et_age;
    private Button bt_save,bt_query,bt_update,bt_delete;
    private TextView tv_show;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化
        init();
        userDao=new UserDao(this);
    }

    public void init(){
        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_password);
        et_age = findViewById(R.id.et_age);
        bt_save = findViewById(R.id.bt_save);
        bt_query = findViewById(R.id.bt_query);
        bt_update = findViewById(R.id.bt_update);
        bt_delete = findViewById(R.id.bt_delete);
        tv_show = findViewById(R.id.tv_show);

        bt_save.setOnClickListener(this);
        bt_query.setOnClickListener(this);
        bt_update.setOnClickListener(this);
        bt_delete.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt_save:{
                user=new User(et_username.getText().toString(),et_password.getText().toString(),Integer.parseInt((et_age.getText().toString())));
                long i=userDao.addUser(user);
                if(i!=-1){
                    Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(this, "添加失敗", Toast.LENGTH_SHORT).show();
                }
                break;
            }
            case R.id.bt_delete:{
                int i=  userDao.deleteUser(et_username.getText().toString());
                if(i!=0){
                    Toast.makeText(this, "刪除成功", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(this, "刪除失敗", Toast.LENGTH_SHORT).show();
                }
                break;
            }
            case R.id.bt_update:{
                user=new User(et_username.getText().toString(),et_password.getText().toString(),Integer.parseInt((et_age.getText().toString())));
                int i=  userDao.updateUser(user);
                if(i!=0){
                    Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(this, "修改失敗", Toast.LENGTH_SHORT).show();
                }
                break;
            }
            case R.id.bt_query:{
            //為了解決查詢重復(fù)問題,需要先創(chuàng)建一個(gè)StringBuffer或者String類型對象,用于存儲數(shù)據(jù),存儲后在給控件賦值就可以解決
                ArrayList list=userDao.queryAll();
                StringBuffer buffer=new StringBuffer();
                if(list.size()==0){
                    tv_show.setText("沒有數(shù)據(jù)");
                }else {
                    for (int i=0;i<list.size();i++){
                        User user= (User) list.get(i);
                        buffer.append("id:" +user.getId()+
                                "用戶名:"+user.getUsername()+
                                "密碼:"+user.getPassword()+
                                "年齡:"+user.getAge()+"\n");
                    }
		                tv_show.setText(buffer);
                }
                break;
            }
        }
    }
}

3、UserDao.java

包含對數(shù)據(jù)庫的增刪改查方法

public class UserDao {
    private SQLiteOpenHelper helper;
    public UserDao(Context context){
        helper=new SQLiteOpenHelper(context,"user1",null,1);
    }
    //添加數(shù)據(jù)
    public long addUser(User user){
        //1.獲取數(shù)據(jù)庫對象
        SQLiteDatabase database=helper.getWritableDatabase();
        //那些列為空,可以設(shè)置為空
        ContentValues values=new ContentValues();
        //key是數(shù)據(jù)表的列名,value是要放進(jìn)去的值
        values.put("username",user.getUsername());
        values.put("password",user.getPassword());
        values.put("age",user.getAge());
        //第一個(gè)參數(shù)表明,第二個(gè)參數(shù)自動賦值為null的列名,第三個(gè)參數(shù)數(shù)據(jù)
        //返回值long,插入成功行號,插入失敗-1
        long i=database.insert("users",null,values);
        //關(guān)閉數(shù)據(jù)庫
        database.close();
        return i;
    }
    //刪除
    public int deleteUser(String username){
        //1.獲取數(shù)據(jù)庫對象
        SQLiteDatabase database=helper.getWritableDatabase();
        //第一個(gè)參數(shù)表明,第二個(gè)參數(shù)為刪除條件,第三個(gè)參數(shù)為第二個(gè)參數(shù)中占位符所需值組成的字符串?dāng)?shù)組
        int i=database.delete("users","username=?",new String[]{username+""});
        //關(guān)閉數(shù)據(jù)庫
        database.close();
        return i;
    }
    //修改
    public int updateUser(User user){
        //1.獲取數(shù)據(jù)庫對象
        SQLiteDatabase database=helper.getWritableDatabase();
        //那些列為空,可以設(shè)置為空
        ContentValues values=new ContentValues();
        //key是數(shù)據(jù)表的列名,value是要放進(jìn)去的值
        values.put("username",user.getUsername());
        values.put("password",user.getPassword());
        values.put("age",user.getAge());
        //第一個(gè)參數(shù)表明,第二個(gè)參數(shù)新數(shù)據(jù),第三個(gè)參數(shù)是條件
        int i=database.update("users",values,"username=?",new String[]{user.getUsername()});
        //關(guān)閉數(shù)據(jù)庫
        database.close();
        return i;
    }
    //查詢
    public ArrayList queryAll(){
        ArrayList list=new ArrayList();
        SQLiteDatabase database=helper.getWritableDatabase();
        Cursor cursor=database.query("users",null,null,null,null,null,null);
        list=convertFromCursor(cursor);
        return list;
    }
    //通過對Cursor對象遍歷查詢結(jié)果,并將其范圍為一個(gè)list集合
    private ArrayList convertFromCursor(Cursor cursor){
        ArrayList list=new ArrayList();
        if(cursor!=null&&cursor.moveToFirst()){
            //通過游標(biāo)遍歷這個(gè)集合
            do{
                int id=cursor.getInt(cursor.getColumnIndex("id"));
                String username=cursor.getString(cursor.getColumnIndex("username"));
                String password=cursor.getString(cursor.getColumnIndex("password"));
                int age=cursor.getInt(cursor.getColumnIndex("age"));
                User user=new User(id,username,password,age);
                list.add(user);
            }while (cursor.moveToNext());
        }
        return list;
    }
}

4、User.java

實(shí)體類對應(yīng)著user表中的字段

public class User {
    private int id;
    private String username;
    private String password;
    private int age;

    public User(int id, String username, String password, int age) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.age = age;
    }

    public User(String username, String password, int age) {
        this.username = username;
        this.password = password;
        this.age = age;
    }
    public User(){};

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

5、SQLiteOpenHelper.java

創(chuàng)建表,更新表方法

public class SQLiteOpenHelper extends android.database.sqlite.SQLiteOpenHelper {
    private Context context;
    public static final String CREATE_TABLES="create table users ("+
            "id integer primary key autoincrement,"+
            "username text,"+
            "password text,"+
            "age integer)";
    public static final String CREATE_DEPARTMENT="create table department ("+
            "id integer primary key autoincrement,"+
            "departmentName text,"+
            "departCode text)";
    public SQLiteOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        this.context=context;
    }
    //創(chuàng)建數(shù)據(jù)表(只有在第一次創(chuàng)建數(shù)據(jù)庫的時(shí)候才會被調(diào)用)
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(CREATE_TABLES);
        Toast.makeText(context, "success databases", Toast.LENGTH_SHORT).show();
    }
    //數(shù)據(jù)庫的更新(第一個(gè)參數(shù)數(shù)據(jù)庫對象,第二個(gè)參數(shù)舊版本號,第三個(gè)參數(shù)新版本號)
    //新版本號大于舊版本號就會調(diào)用onUpgrade方法
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        /**
         * //如果數(shù)據(jù)表存在就刪除
         *         sqLiteDatabase.execSQL("drop table if exists users");
         *         sqLiteDatabase.execSQL("drop table if exists department");
         *         onCreate(sqLiteDatabase);
          */
        switch (i){
            case 1:sqLiteDatabase.execSQL(CREATE_DEPARTMENT);
        }

    }
}

總結(jié)

到此這篇關(guān)于Android數(shù)據(jù)庫增刪改查的文章就介紹到這了,更多相關(guān)Android數(shù)據(jù)庫增刪改查內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Kotlin中l(wèi)et()with()run()apply()also()函數(shù)的使用方法與區(qū)別

    Kotlin中l(wèi)et()with()run()apply()also()函數(shù)的使用方法與區(qū)別

    在Kotlin中的源碼標(biāo)準(zhǔn)庫(Standard.kt)中提供了一些Kotlin擴(kuò)展的內(nèi)置函數(shù)可以優(yōu)化kotlin的編碼,今天為大家聊聊let,with,run,apply,also幾個(gè)函數(shù)的用法與區(qū)別
    2018-03-03
  • Android Uri和文件路徑互相轉(zhuǎn)換的實(shí)例代碼

    Android Uri和文件路徑互相轉(zhuǎn)換的實(shí)例代碼

    在項(xiàng)目中需要用到將Uri轉(zhuǎn)換為絕對路徑,下面小編把Android Uri和文件路徑互相轉(zhuǎn)換的實(shí)例代碼分享到腳本之家平臺,需要的的朋友參考下吧
    2017-07-07
  • Flutter中數(shù)據(jù)庫的使用教程詳解

    Flutter中數(shù)據(jù)庫的使用教程詳解

    在Flutter開發(fā)過程中,有時(shí)需要對數(shù)據(jù)進(jìn)行本地的持久化存儲,使用sp文件形式雖然也能解決問題,但是有時(shí)數(shù)據(jù)量較大的時(shí)候,顯然我們文件形式就不太合適了,這時(shí)候我們就需要使用數(shù)據(jù)庫進(jìn)行存儲。本文將詳細(xì)講講Flutter中數(shù)據(jù)庫的使用,需要的可以參考一下
    2022-04-04
  • Android加載圖片內(nèi)存溢出問題解決方法

    Android加載圖片內(nèi)存溢出問題解決方法

    這篇文章主要介紹了Android加載圖片內(nèi)存溢出問題解決方法,本文講解使用BitmapFactory.Options解決內(nèi)存溢出問題,需要的朋友可以參考下
    2015-06-06
  • Kotlin線程的橋接與切換使用介紹

    Kotlin線程的橋接與切換使用介紹

    這篇文章主要介紹了Android開發(fā)中Kotlin線程的橋接與切換,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • android studio 4.0 新建類沒有修飾符的方法

    android studio 4.0 新建類沒有修飾符的方法

    這篇文章主要介紹了android studio 4.0 新建類沒有修飾符的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • Android筆記之:App調(diào)試的幾個(gè)命令的實(shí)踐與分析

    Android筆記之:App調(diào)試的幾個(gè)命令的實(shí)踐與分析

    本篇文章介紹了,在Android中:App調(diào)試的幾個(gè)命令的實(shí)踐與分析。需要的朋友參考下
    2013-04-04
  • Android TV 3D卡片無限循環(huán)效果

    Android TV 3D卡片無限循環(huán)效果

    這篇文章主要為大家詳細(xì)介紹了Android TV 3D卡片無限循環(huán)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Android使用Spinner實(shí)現(xiàn)城市級聯(lián)下拉框

    Android使用Spinner實(shí)現(xiàn)城市級聯(lián)下拉框

    這篇文章主要為大家詳細(xì)介紹了Android使用Spinner實(shí)現(xiàn)城市級聯(lián)下拉框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android實(shí)現(xiàn)畫板、寫字板功能(附源碼下載)

    Android實(shí)現(xiàn)畫板、寫字板功能(附源碼下載)

    這篇文章主要介紹了Android實(shí)現(xiàn)畫板、寫字板功能的方法,文中給出了簡單的介紹和示例代碼,想要了解更多的朋友可以下載源碼進(jìn)行學(xué)習(xí),感興趣的朋友們下面來一起看看吧。
    2017-01-01

最新評論

乌拉特后旗| 满洲里市| 二手房| 工布江达县| 甘泉县| 炎陵县| 台东市| 襄樊市| 丹寨县| 普格县| 虎林市| 县级市| 博客| 三江| 富锦市| 繁昌县| 新巴尔虎左旗| 漳浦县| 瑞安市| 平湖市| 行唐县| 玉龙| 东港市| 邢台市| 丹巴县| 五莲县| 湛江市| 宣汉县| 金门县| 西吉县| 武强县| 遂宁市| 江达县| 凤阳县| 竹北市| 共和县| 庆云县| 大庆市| 龙州县| 哈尔滨市| 白水县|