从零开始学安卓笔记:Android数据库SQLite的增删改查

前段时间准备系统学习一下Android的知识点,结果学了部分之后天天加班,搁置了有两个月了,中间有些知识点也没有发文记录。昨天学习了数据库的操作,最基础的增删改查功能,由于昨天跟着视频敲的时候有些困,最后的布局没有完善。记录一下所有的代码:

activity_main.xml内容:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="60dp"
            android:orientation="vertical">
        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:textSize="20dp"
                    android:text="账号:">
            </TextView>
            <EditText
                    android:id="@+id/name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:textSize="20dp"
                    android:hint="请输入"/>
        </LinearLayout>
        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:textSize="20dp"
                    android:text="密码:">
            </TextView>
            <EditText
                    android:id="@+id/psw"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:textSize="20dp"
                    android:hint="请输入"/>
        </LinearLayout>
        <Button
                android:id="@+id/add"
                android:layout_marginTop="40dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="20dp"
                android:text="添加"/>
        <Button
                android:id="@+id/del"
                android:layout_marginTop="40dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="20dp"
                android:text="删除"/>
        <Button
                android:id="@+id/change"
                android:layout_marginTop="40dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="20dp"
                android:text="修改"/>
        <Button
                android:id="@+id/seeAll"
                android:layout_marginTop="40dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="20dp"
                android:text="查询"/>
        <ScrollView
                android:layout_width="match_parent"
                android:layout_marginTop="40dp"
                android:layout_height="150dp">
            <TextView
                    android:id="@+id/content"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
        </ScrollView>
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java内容:

package com.jasmyn.database;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    EditText name,psw;
    Button add,del,change,seeAll;
    TextView content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        name = findViewById(R.id.name);
        psw = findViewById(R.id.psw);
        add = findViewById(R.id.add);
        del = findViewById(R.id.del);
        change = findViewById(R.id.change);
        seeAll = findViewById(R.id.seeAll);
        content = findViewById(R.id.content);

        final DB db = new DB(this);

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String n = name.getText().toString();
                String p = psw.getText().toString();
                if (db.add(n,p)) {
                    Toast.makeText(MainActivity.this,"添加成功",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(MainActivity.this,"添加失败",Toast.LENGTH_SHORT).show();
                }
            }
        });
        del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String n = name.getText().toString();
                String p = psw.getText().toString();
                if (db.del(n)) {
                    Toast.makeText(MainActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(MainActivity.this,"删除失败",Toast.LENGTH_SHORT).show();
                }
            }
        });
        change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String n = name.getText().toString();
                String p = psw.getText().toString();
                if (db.change(n,p)) {
                    Toast.makeText(MainActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(MainActivity.this,"修改失败",Toast.LENGTH_SHORT).show();
                }
            }
        });
        seeAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList a = db.getAll();
                for (Object object:
                        a) {
                    String n = content.getText().toString();
                    String str =n + ((User)object).toString()+"\n";
                    content.setText(str);
                }


            }
        });
    }
}

DB.java内容:

package com.jasmyn.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import androidx.annotation.Nullable;

import java.util.ArrayList;

public class DB extends SQLiteOpenHelper {
    public DB(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    private SQLiteDatabase dp;

    public DB(Context context) {
        super(context,"TEST1.dp",null,1);
        dp = this.getWritableDatabase();
    }

    public boolean add(String name,String psw) {
        ContentValues values = new ContentValues();
        values.put("name",name);
        values.put("psw",psw);
        long i = dp.insert("users",null,values);
        if (i > 0) {
            Log.d("","插入成功");
            return true;
        }
        return false;
    }

    public boolean del(String name) {
        long i = dp.delete("users","name=?",new String[]{name});
        if (i > 0) {
            Log.d("","删除成功");
            return true;
        }
        return false;
    }

    public boolean change(String name,String NewPsw) {
        ContentValues values = new ContentValues();
        values.put("psw",NewPsw);
        long i = dp.update("users",values,"name=?",new String[]{name});
        if (i > 0) {
            Log.d("","修改成功");
            return true;
        }
        return false;
    }

    public ArrayList getAll() {
        ArrayList arrayList = new ArrayList();
        Cursor cursor = dp.query("users",null,null,null,null,null,null);
        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String psw = cursor.getString(cursor.getColumnIndex("psw"));
            User u = new User(name,psw);
            arrayList.add(u);
        }
        return arrayList;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "create table users(name text primary key,psw text not null)";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

User.java内容:

package com.jasmyn.database;

public class User {

    String name;
    String psw;

    public User(String name, String psw) {
        this.name = name;
        this.psw = psw;
    }

    @Override
    public String toString() {
        return name + "," + psw;
    }
}

后面实例写完了再继续更新。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jasmyn518

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值