Android开发实现增删改查

Android开发实现增删改查,第1张

创建数据库并写入方法
package com.example.sql;
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 java.util.ArrayList;
import java.util.List;

public class MySQLliteOpenHelper extends SQLiteOpenHelper{
    SQLiteDatabase userDatabase;
    private static final String DB_NAME="sqls.db";
    private static final String TABLE_NAME_STUDENT="student";
    private static final String CREATE_TABLE_SQL_STUDENT="create table student("+
            "id integer primary key autoincrement,name text,num text)";
    public MySQLliteOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }
    public MySQLliteOpenHelper(@Nullable Context context) {
        super(context,DB_NAME,null,1);
    }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(CREATE_TABLE_SQL_STUDENT);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
    //添加
    public long insertData(Student student) {
        SQLiteDatabase db=getWritableDatabase();
        ContentValues contentValues=new ContentValues();
        contentValues.put("name",student.getName());
        contentValues.put("num",student.getNum());


        return db.insert(TABLE_NAME_STUDENT,null,contentValues);
    }
    //删除
    public int deleteDataByName(String name){
        SQLiteDatabase db=getWritableDatabase();
        return db.delete(TABLE_NAME_STUDENT,"name=?",new String[]{name});
    }
    //修改
    public int update(Student student){
        SQLiteDatabase db=getWritableDatabase();
        ContentValues contentValues=new ContentValues();
        contentValues.put("name",student.getName());
        contentValues.put("num",student.getNum());
        return db.update(TABLE_NAME_STUDENT,contentValues,"name like ?",new String[]{student.getName()});
    }
    //查找
    public List<Student> selectFromName(String name){
        SQLiteDatabase db=getReadableDatabase();
        List<Student> studentList=new ArrayList<>();
        Cursor c=db.query(TABLE_NAME_STUDENT,null,"name like ?",new String[]{name},null,null,null);
        if(c!=null){
            while(c.moveToNext()){
                String stuName=c.getString(c.getColumnIndex("name"));
                String stuNum=c.getString(c.getColumnIndex("num"));
                Student student=new Student();
                student.setName(stuName);
                student.setNum(stuNum);

                studentList.add(student);
            }
            c.close();
        }
        return studentList;
    }


    //显示所有数据
    public List<Student> selectall(String name){
        SQLiteDatabase db=getReadableDatabase();
        List<Student> studentList=new ArrayList<>();
        Cursor c = db.query("student", null, null, null, null, null, null, null);
        if(c!=null){
            while(c.moveToNext()){
                String stuName=c.getString(c.getColumnIndex("name"));
                String stuNum=c.getString(c.getColumnIndex("num"));
                Student student=new Student();
                student.setName(stuName);
                student.setNum(stuNum);

                studentList.add(student);
            }
            c.close();
        }
        return studentList;
    }



}

方法调用

增加

package com.example.sql;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.sql.Student;


import android.widget.Toast;

public class InsertActivity extends AppCompatActivity {
    private EditText stuName,stuId;
    private Button btnHold;
    private MySQLliteOpenHelper mySQLliteOpenHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insert);

        initView();

        mySQLliteOpenHelper=new MySQLliteOpenHelper(this);
        btnHold.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name=stuName.getText().toString().trim();
                String num=stuId.getText().toString().trim();

                Student student=new Student();
                student.setName(name);
                student.setNum(num);

                long rowId=mySQLliteOpenHelper.insertData(student);
                if(rowId!=-1){
                    ToastUtill.toastshort(InsertActivity.this,"添加成功!");
                }else{
                    ToastUtill.toastshort(InsertActivity.this,"添加失败!");

                }
            }
        });
    }

    private void initView() {
        stuName=findViewById(R.id.stu_name);
        stuId=findViewById(R.id.stu_id);
        btnHold=findViewById(R.id.btn_hold);
    }


}

删除

package com.example.sql;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class DeleteActivity extends AppCompatActivity {
    EditText inname;
    private MySQLliteOpenHelper mMySQLliteOpenHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete);
        mMySQLliteOpenHelper = new MySQLliteOpenHelper(this);
        initView();
    }

    private void initView() {
        inname=findViewById(R.id.stu_name);
    }

    public void deleteOnClick(View view) {
        String name=inname.getText().toString().trim();
        int i=mMySQLliteOpenHelper.deleteDataByName(name);
        if(i>0){
            ToastUtill.toastshort(this,"删除成功!删除了"+i+"行");
        }else{
            ToastUtill.toastshort(this,"删除失败!!");

        }
    }
}

修改

package com.example.sql;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class UpdateActivity extends AppCompatActivity {
    EditText stuName,stuNum;
    private MySQLliteOpenHelper mySQLliteOpenHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);
        mySQLliteOpenHelper=new MySQLliteOpenHelper(this);
        initView();
    }

    private void initView() {
        stuName=findViewById(R.id.stu_name);
        stuNum=findViewById(R.id.stu_num);
    }

    public void updateOnClick(View view) {
        String name=stuName.getText().toString().trim();
        String num=stuNum.getText().toString().trim();

        Student student=new Student();
        student.setName(name);
        student.setNum(num);
        int i=mySQLliteOpenHelper.update(student);
        if(i>0){
            ToastUtill.toastshort(this,"修改成功!");
        }else{
            ToastUtill.toastshort(this,"修改失败!!!");
        }
    }
}

查询

package com.example.sql;

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

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

public class SelectActivity extends AppCompatActivity {
    Button btnSelectName;
    TextView toname,edinto;
    ListView lvSelect;
    private MySQLliteOpenHelper mySQLliteOpenHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select);
        mySQLliteOpenHelper=new MySQLliteOpenHelper(this);
        initView();
    }

    private void initView() {
        btnSelectName=findViewById(R.id.select_name);
        toname=findViewById(R.id.stu_name);
        edinto=findViewById(R.id.edinfo);
//        lvSelect=findViewById(R.id.lv_select);
    }

    public void selectOnClick(View view) {
        String name=toname.getText().toString().trim();
        if(!TextUtils.isEmpty(name)){
            List<Student> students=mySQLliteOpenHelper.selectFromName(name);
            if(!students.isEmpty()){
                showData(students);
                return;
            }else{
                ToastUtill.toastshort(this,"查询结果为空!");
            }

        }
    }

    private void showData(@NonNull List<Student> students){
        StringBuilder stringBuilder=new StringBuilder();
        for(Student student1:students){
            stringBuilder.append("姓名:");
            stringBuilder.append(student1.getName());
            stringBuilder.append(",学号:");
            stringBuilder.append(student1.getNum());
            stringBuilder.append("\n");
        }
        edinto.setText(stringBuilder.toString());
    }


}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/876502.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存