Android Studio实现数据库的所有操作

采用android studio自带的数据库实现stu数据库和stu表的创建,增删改查和关闭

这是项目的大致结构

主界面

子界面

布局源码

<!-- Main -->
<?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="vertical"
    >
    <Button
        android:id="@+id/b0"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:text="创建学生信息表"
        android:layout_margin="10dp"/>
    <Button
        android:id="@+id/b1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:text="增加数据"
        android:layout_margin="10dp"
        />
    <Button
        android:id="@+id/b2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:text="删除数据"
        android:layout_margin="10dp"
        />
    <Button
        android:id="@+id/b3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:text="修改数据"
        android:layout_margin="10dp"
        />
    <Button
        android:id="@+id/b4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:text="查询数据"
        android:layout_margin="10dp"
        />
    <Button
        android:id="@+id/b5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:text="浏览数据"
        android:layout_margin="10dp"
        />
    <Button
        android:id="@+id/b6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:text="清空表格"
        android:layout_margin="10dp"
        />
    <Button
        android:id="@+id/b7"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:text="关闭数据库"
        android:layout_margin="10dp"
        />
</LinearLayout>

<!-- Add -->
<?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="vertical"
    >
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="10pt"
    android:layout_margin="10dp"
    android:text="请输入学号"
    android:textColor="@android:color/holo_blue_bright"
    />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:inputType="text"
        android:layout_margin="10dp"
        android:hint="请输入学号"
        android:id="@+id/id"
        />
    <TextView
        android:textColor="@android:color/holo_blue_bright"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:layout_margin="10dp"
        android:text="请输入姓名"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:inputType="text"
        android:layout_margin="10dp"
        android:hint="请输入姓名"
        android:id="@+id/name"
        />
    <TextView
        android:textColor="@android:color/holo_blue_bright"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:layout_margin="10dp"
        android:text="请输入年龄"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:layout_margin="10dp"
        android:inputType="text"
        android:hint="请输入年龄"
        android:id="@+id/age"
        />
    <TextView
        android:textColor="@android:color/holo_blue_bright"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:text="请选择性别"
        />
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:id="@+id/sex"
        android:orientation="horizontal"
        >
        <RadioButton
            android:id="@+id/man"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10pt"
            android:layout_margin="10dp"
            android:text="男"
            />
        <RadioButton
            android:id="@+id/woman"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10pt"
            android:layout_margin="10dp"
            android:text="女"
            />
    </RadioGroup>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:layout_margin="10dp"
        android:text="添加"
        android:id="@+id/add"
        />
</LinearLayout>

<!-- Delete -->
<?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="vertical"
    >
    <TextView
        android:textColor="@android:color/holo_blue_bright"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:layout_margin="10dp"
        android:text="通过学号删除"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:layout_margin="10dp"
        android:inputType="text"
        android:hint="请输入学号"
        android:id="@+id/id"
        />

    <Button
        android:textColor="@android:color/holo_red_dark"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:layout_margin="10dp"
        android:text="删除"
        android:id="@+id/delete"
        />
</LinearLayout>

<!-- Update -->
<?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="vertical"
    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:layout_margin="5dp"
        android:text="请输入旧学号"
        android:textColor="@android:color/holo_blue_bright"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:inputType="text"
        android:layout_margin="5dp"
        android:hint="请输入旧学号"
        android:id="@+id/oldid"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:layout_margin="5dp"
        android:text="请输入学号"
        android:textColor="@android:color/holo_blue_bright"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:inputType="text"
        android:layout_margin="5dp"
        android:hint="请输入学号"
        android:id="@+id/id"
        />
    <TextView
        android:textColor="@android:color/holo_blue_bright"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:layout_margin="5dp"
        android:text="请输入姓名"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:inputType="text"
        android:layout_margin="5dp"
        android:hint="请输入姓名"
        android:id="@+id/name"
        />
    <TextView
        android:textColor="@android:color/holo_blue_bright"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:layout_margin="5dp"
        android:text="请输入年龄"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:layout_margin="5dp"
        android:inputType="text"
        android:hint="请输入年龄"
        android:id="@+id/age"
        />
    <TextView
        android:textColor="@android:color/holo_blue_bright"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:text="请选择性别"
        />
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:id="@+id/sex"
        android:orientation="horizontal"
        >
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10pt"
            android:layout_margin="5dp"
            android:text="男"
            />
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10pt"
            android:layout_margin="5dp"
            android:text="女"
            />
    </RadioGroup>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:layout_margin="5dp"
        android:text="修改"
        android:id="@+id/update"
        />

</LinearLayout>

<!-- Select -->
<?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="vertical"
    >
    <TextView
        android:textColor="@android:color/holo_blue_bright"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:layout_margin="10dp"
        android:text="通过学号查询"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:layout_margin="10dp"
        android:inputType="text"
        android:hint="请输入学号"
        android:id="@+id/id"
        />

    <Button
        android:textColor="@android:color/holo_red_dark"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:layout_margin="10dp"
        android:text="查询"
        android:id="@+id/select"
        />
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list"
        android:divider="@color/black"
        />

</LinearLayout>

<!-- Browse -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    >
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list"
        android:divider="@color/black"
        />


</LinearLayout>

<!-- StuAdapter -->

<?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"
    android:background="@color/black"
    >
    <TextView
        android:id="@+id/t1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_margin="2dp"
        android:background="@color/white"
        />
    <TextView
        android:id="@+id/t2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_margin="2dp"
        android:background="@color/white"
        />
    <TextView
        android:id="@+id/t3"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_margin="2dp"
        android:background="@color/white"
        />
    <TextView
        android:id="@+id/t4"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_margin="2dp"
        android:background="@color/white"
        />
</LinearLayout>

后台JAVA代码

//Stu
package com.example.stuapp.pojo;


public class Stu {
    private String id,name,age,sex;

    public Stu(String id, String name, String age, String sex) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public Stu() {
    }

    @Override
    public String toString() {
        return "Stu{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

//StuHelp

package com.example.stuapp.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import com.example.stuapp.MainActivity;
import com.example.stuapp.pojo.Stu;

import java.util.ArrayList;
import java.util.List;


public class StuHelp extends SQLiteOpenHelper {
    private static StuHelp stuHelp;
    private static final String Name="stu";


    public static StuHelp getStuHelp(Context context) {
        if(stuHelp==null) stuHelp=new StuHelp(context);
        return stuHelp;
    }
    public long add(SQLiteDatabase sqLiteDatabase, Stu stu){
        System.out.println(stu.toString());
        ContentValues values=new ContentValues();
        values.put("id",stu.getId());
        values.put("name",stu.getName());
        values.put("age",stu.getAge());
        values.put("sex",stu.getSex());
        return sqLiteDatabase.insert(Name,null,values);
    }
public long delete(SQLiteDatabase sqLiteDatabase,String id){
        return sqLiteDatabase.delete(Name,"id=?",new String[]{id});
}
public long update(SQLiteDatabase sqLiteDatabase,Stu stu,String id){
        ContentValues values=new ContentValues();
        if(stu.getId()!=null&&!stu.getId().equals(""))
        values.put("id",stu.getId());
        if(stu.getName()!=null&&!stu.getName().equals(""))
        values.put("name",stu.getName());
        if(stu.getAge()!=null&&!stu.getAge().equals(""))
        values.put("age",stu.getAge());
        if(stu.getSex()!=null&&!stu.getSex().equals(""))
        values.put("sex",stu.getSex());

        return sqLiteDatabase.update(Name,values,"id=?",new String[]{id});
}
public Stu select(SQLiteDatabase sqLiteDatabase,String id){
        Cursor cursor=sqLiteDatabase.rawQuery("select * from stu where id=?",new String[]{id});
        Stu stu=null;
        if(cursor.getCount()>0){ cursor.moveToNext();stu=new Stu(cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getString(3));}
         return stu;
}
public List<Stu> browse(SQLiteDatabase sqLiteDatabase){
        List<Stu> stus=new ArrayList<>();
       Cursor cursor =sqLiteDatabase.query(Name,null,null,null,null,null,null);
       while (cursor.moveToNext()){
           Stu stu=new Stu();
            stu.setId(cursor.getString(0));
            stu.setName(cursor.getString(1));
            stu.setAge(cursor.getString(2));
            stu.setSex(cursor.getString(3));
            stus.add(stu);
        }
        return stus;
}
    private StuHelp(@Nullable Context context) {
        super(context,Name ,null, 1);
    }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String sql="create table if not exists stu(" +
         "id varchar(10) not null primary key," +
         "`name` varchar(10) not null," +
         "age varchar(10) not null," +
         "sex varchar(10) not null)";
        sqLiteDatabase.execSQL(sql);
    }
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}

//StuAdapter

package com.example.stuapp.utils;

import android.app.Application;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.stuapp.R;
import com.example.stuapp.pojo.Stu;

import java.util.List;

public class StuAdapter extends BaseAdapter {
    private Context context;
    private List<Stu> stus;
    public StuAdapter(Context context, List<Stu> stus) {
        super();
        this.context=context;
        this.stus=stus;

    }

    @Override
    public int getCount() {
        return stus.size();
    }

    @Override
    public Object getItem(int i) {
        return stus.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
      View stuview = LayoutInflater.from(context).inflate(R.layout.stu_item,null);
        TextView[] textView=new TextView[9];
        Stu stu=stus.get(i);
        textView[0]=stuview.findViewById(R.id.t1);
        textView[0].setText(stu.getId());
        textView[1]=stuview.findViewById(R.id.t2);
        textView[1].setText(stu.getName());
        textView[2]=stuview.findViewById(R.id.t3);
        textView[2].setText(stu.getAge());
        textView[3]=stuview.findViewById(R.id.t4);
        textView[3].setText(stu.getSex());
        return stuview;
    }
}

//Main

package com.example.stuapp;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.stuapp.utils.StuHelp;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private String SqlPath;
    private SQLiteDatabase sqLiteDatabase;
    private StuHelp stuHelp;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button[] buttons=new Button[10];
        SqlPath=getFilesDir()+"/stu.db";
       buttons[0]=findViewById(R.id.b0);
       buttons[0].setOnClickListener(this);
        buttons[1]=findViewById(R.id.b1);
        buttons[1].setOnClickListener(this);
        buttons[2]=findViewById(R.id.b2);
        buttons[2].setOnClickListener(this);
        buttons[3]=findViewById(R.id.b3);
        buttons[3].setOnClickListener(this);
        buttons[4]=findViewById(R.id.b4);
        buttons[4].setOnClickListener(this);
        buttons[5]=findViewById(R.id.b5);
        buttons[5].setOnClickListener(this);
        buttons[6]=findViewById(R.id.b6);
        buttons[6].setOnClickListener(this);
       buttons[7]=findViewById(R.id.b7);
       buttons[7].setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        sqLiteDatabase=openOrCreateDatabase(SqlPath, Context.MODE_PRIVATE,null);
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        Intent intent=new Intent();
        switch (view.getId()){
            case R.id.b0:
                stuHelp=StuHelp.getStuHelp(getApplicationContext());
                stuHelp.onCreate(sqLiteDatabase);
                builder.setTitle("创建结果")
                        .setMessage("信息表路径为"+sqLiteDatabase.getPath().toString())
                        .setPositiveButton("确认",null)
                        .show();
                break;
            case R.id.b1:
                intent.setClass(MainActivity.this,Add.class);
                startActivity(intent);
                break;
            case R.id.b2:
                intent.setClass(MainActivity.this,Delete.class);
                startActivity(intent);
                break;
            case R.id.b3:
                intent.setClass(MainActivity.this,Update.class);
                startActivity(intent);
                break;
            case R.id.b4:
                intent.setClass(MainActivity.this,Select.class);
                startActivity(intent);
                break;
            case R.id.b5:
                intent.setClass(MainActivity.this,Browse.class);
                startActivity(intent);
                break;
            case R.id.b6:
                builder.setTitle("清空表格")
                        .setMessage("表格已经清空")
                        .setPositiveButton("确认",null)
                        .show();
                sqLiteDatabase.execSQL("delete from stu where id=id");
                break;
            case R.id.b7:
                builder.setTitle("关闭结果")
                        .setMessage("数据库已经关闭")
                        .setPositiveButton("确认",null)
                        .show();
                sqLiteDatabase.close();
                break;
        }
    }
}

//Add

package com.example.stuapp;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.stuapp.pojo.Stu;
import com.example.stuapp.utils.StuHelp;

public class Add extends AppCompatActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
    private Stu stu;
    private String SqlPath;
    private SQLiteDatabase sqLiteDatabase;
    private StuHelp stuHelp;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add);
        Button add=findViewById(R.id.add);
        stu=new Stu();
        add.setOnClickListener(this);
        SqlPath=getFilesDir()+"/stu.db";
        RadioGroup sex=findViewById(R.id.sex);
        sex.setOnCheckedChangeListener(this);

    }

    @Override
    public void onClick(View view) {
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("添加结果");
        EditText id=findViewById(R.id.id);
        stu.setId(id.getText().toString());
        EditText name=findViewById(R.id.name);
        stu.setName(name.getText().toString());
        EditText age=findViewById(R.id.age);
        stu.setAge(age.getText().toString());
        sqLiteDatabase=openOrCreateDatabase(SqlPath, Context.MODE_PRIVATE,null);
        stuHelp=StuHelp.getStuHelp(getApplicationContext());
        long row=stuHelp.add(sqLiteDatabase,stu);
        if(row>0){
            builder.setMessage("添加成功"+" "+stu.toString());
        }
        else{
            builder.setMessage("添加失败"+" "+stu.toString());
        }
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Intent intent=new Intent();
                intent.setClass(Add.this,MainActivity.class);
                startActivity(intent);
            }
        });
        builder.show();
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.man:
            stu.setSex("男");
            break;
            case R.id.woman:
            stu.setSex("女");
            break;
        }
    }
}

//Delete

package com.example.stuapp;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.stuapp.pojo.Stu;
import com.example.stuapp.utils.StuHelp;

public class Delete extends AppCompatActivity implements View.OnClickListener {
    private String SqlPath;
    private SQLiteDatabase sqLiteDatabase;
    private StuHelp stuHelp;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.delete);
        Button delete=findViewById(R.id.delete);
        delete.setOnClickListener(this);
        SqlPath=getFilesDir()+"/stu.db";
    }

    @Override
    public void onClick(View view) {
        EditText id=findViewById(R.id.id);
        sqLiteDatabase=openOrCreateDatabase(SqlPath, Context.MODE_PRIVATE,null);
        stuHelp=StuHelp.getStuHelp(getApplicationContext());
        long row=stuHelp.delete(sqLiteDatabase,id.getText().toString());
        String res;
        if(row>0) res="删除成功";
        else  res="删除失败";
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("删除结果")
                .setMessage(res)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Intent intent=new Intent();
                        intent.setClass(Delete.this,MainActivity.class);
                        startActivity(intent);
                    }
                })
                .show();
    }
}

//Update

package com.example.stuapp;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.stuapp.pojo.Stu;
import com.example.stuapp.utils.StuHelp;

public class Update extends AppCompatActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
    private Stu stu;
    private String SqlPath;
    private SQLiteDatabase sqLiteDatabase;
    private StuHelp stuHelp;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.update);
        Button update=findViewById(R.id.update);
        update.setOnClickListener(this);
        stu=new Stu();
        SqlPath=getFilesDir()+"/stu.db";
        RadioGroup sex=findViewById(R.id.sex);
        sex.setOnCheckedChangeListener(this);
    }

    @Override
    public void onClick(View view) {
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        sqLiteDatabase=openOrCreateDatabase(SqlPath, Context.MODE_PRIVATE,null);
        stuHelp=StuHelp.getStuHelp(getApplicationContext());
        EditText id=findViewById(R.id.id);
        stu.setId(id.getText().toString());
        EditText name=findViewById(R.id.name);
        stu.setName(name.getText().toString());
        EditText age=findViewById(R.id.age);
        stu.setAge(age.getText().toString());
        EditText oldid=findViewById(R.id.oldid);
        long row=stuHelp.update(sqLiteDatabase,stu,oldid.getText().toString());
        String res;
        if(row>0) res="修改成功";
        else res="修改失败";
        builder.setTitle("删除结果")
                .setMessage(res)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Intent intent=new Intent();
                        intent.setClass(Update.this,MainActivity.class);
                        startActivity(intent);
                    }
                })
                .show();

    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.man:
                stu.setSex("男");
                break;
            case R.id.woman:
                stu.setSex("女");
                break;
        }
    }
}

//Select

package com.example.stuapp;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.stuapp.pojo.Stu;
import com.example.stuapp.utils.StuAdapter;
import com.example.stuapp.utils.StuHelp;

import java.util.ArrayList;
import java.util.List;

public class Select extends AppCompatActivity implements View.OnClickListener {
    private String SqlPath;
    private SQLiteDatabase sqLiteDatabase;
    private StuHelp stuHelp;
    public List<Stu> stus;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select);
        Button select=findViewById(R.id.select);
        select.setOnClickListener(this);
        SqlPath=getFilesDir()+"/stu.db";

    }

    @Override
    public void onClick(View view) {
        EditText id=findViewById(R.id.id);
        sqLiteDatabase=openOrCreateDatabase(SqlPath,Context.MODE_PRIVATE,null);
        stuHelp=StuHelp.getStuHelp(this);
        stus=new ArrayList<>();
        stus.add(new Stu("学号","姓名","年龄","性别"));
        Stu stu=stuHelp.select(sqLiteDatabase,id.getText().toString());
        if(stu!=null)
        stus.add(stu);
        else{
            AlertDialog.Builder builder=new AlertDialog.Builder(this);
            builder.setTitle("查询失败")
                    .setMessage("未找到相关信息")
                    .setPositiveButton("确认",null)
                    .show();
        }
        StuAdapter stuAdapter=new StuAdapter(this,stus);
        ListView listView=findViewById(R.id.list);
        listView.setAdapter(stuAdapter);
    }
}


//Browse

package com.example.stuapp;


import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.stuapp.pojo.Stu;
import com.example.stuapp.utils.StuAdapter;
import com.example.stuapp.utils.StuHelp;

import java.util.List;

public class Browse extends AppCompatActivity {
    private List<Stu> stus;
    private SQLiteDatabase sqLiteDatabase;
    private StuHelp stuHelp;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.browse);
       ListView listView=findViewById(R.id.list);
        sqLiteDatabase=openOrCreateDatabase(getFilesDir()+"/stu.db", Context.MODE_PRIVATE,null);
        stuHelp=StuHelp.getStuHelp(this);
        stus=stuHelp.browse(sqLiteDatabase);
        stus.add(0,new Stu("学号","姓名","年龄","性别"));
        System.out.println(stus.toString());
        StuAdapter stuAdapter=new StuAdapter(this,stus);
        listView.setAdapter(stuAdapter);
        System.out.println(stus.toString());

    }
}

 差点淹死在学习的海洋里

posted @ 2023-01-13 00:07  突破铁皮  阅读(1017)  评论(0编辑  收藏  举报