Android增删查-项目7

代码如下: MyDBpenHelper.java

package com.example.studentinfotest;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDBpenHelper extends SQLiteOpenHelper{
    private static final String DBNAME = "student.db";
    private static final int VERSION = 1;
    public MyDBpenHelper(Context context) {
        super(context, DBNAME, null, VERSION);
    }
    //创建数据库
    @Override
    public void onCreate(SQLiteDatabase db) {
        //创建数据表
        db.execSQL("create table stu_info(id INTEGER primary key autoincrement,sno varchar(10),name varchar(10),sex varchar(4),professional varchar(10),department varchar(20))");
    }
    //升级数据库
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="信息添加页面"
        android:textSize="30sp"
        android:textStyle="bold"
        android:textColor="#000000"
        android:layout_gravity="center"
        android:layout_margin="80dp"/>
    <EditText
        android:id="@+id/editText_onesno"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="学号"
        android:textSize="25sp"
        android:textColor="#000000"/>
    <EditText
        android:id="@+id/editText_onesname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="姓名"
        android:textSize="25sp"
        android:textColor="#000000"/>
    <EditText
        android:id="@+id/editText_onesex"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="性别"
        android:textSize="25sp"
        android:textColor="#000000"/>
    <EditText
        android:id="@+id/editText_onepro"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="专业班级"
        android:textSize="25sp"
        android:textColor="#000000"/>
    <EditText
        android:id="@+id/editText_onedep"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="所属院部"
        android:textSize="25sp"
        android:textColor="#000000"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/button_oneadd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="添加"
            android:textSize="25sp"
            android:textColor="#000000"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/oneclear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="清除"
            android:textSize="25sp"
            android:textColor="#000000"
            android:layout_weight="1"/>
    </LinearLayout>
    <Button
        android:id="@+id/onenext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下一页"
        android:textSize="25sp"
        android:textColor="#000000"
        android:layout_gravity="right"
        android:layout_marginTop="30dp"/>
</LinearLayout>

MainActivity.class

package com.example.studentinfotest;
import android.content.ContentValues;
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.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
    private EditText edit_sno,edit_onename,edit_onesex,edit_onepro,edit_onedep;
    private Button btn_oneadd,btn_oneclear,btn_onenext;
    private MyDBpenHelper mhelper;
    private SQLiteDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        btnAdd();
        btnClear();
    }
    private void initView() {
        edit_sno = findViewById(R.id.editText_onesno);
        edit_onename = findViewById(R.id.editText_onesname);
        edit_onesex = findViewById(R.id.editText_onesex);
        edit_onepro = findViewById(R.id.editText_onepro);
        edit_onedep = findViewById(R.id.editText_onedep);
        btn_oneadd = findViewById(R.id.button_oneadd);
        btn_oneclear = findViewById(R.id.oneclear);
        btn_onenext = findViewById(R.id.onenext);
        mhelper=new MyDBpenHelper(MainActivity.this);
        db=mhelper.getWritableDatabase();
    }
    private void btnAdd(){
        btn_oneadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ContentValues values = new ContentValues();
                values.put("sno",edit_sno.getText().toString());
                values.put("name",edit_onename.getText().toString());
                values.put("sex",edit_onesex.getText().toString());
                values.put("professional",edit_onepro.getText().toString());
                values.put("department",edit_onedep.getText().toString());
                db.insert("stu_info",null,values);
                Toast.makeText(MainActivity.this,"添加成功",Toast.LENGTH_SHORT).show();
            }
        });
    }
    private void btnClear(){
        btn_oneclear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edit_sno.setText("");
                edit_onename.setText("");
                edit_onesex.setText("");
                edit_onepro.setText("");
                edit_onedep.setText("");
            }
        });
        btn_onenext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,SecondActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
}

activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SecondActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="信息查询页面"
        android:textSize="30sp"
        android:textStyle="bold"
        android:textColor="#000000"
        android:layout_gravity="center"
        android:layout_margin="80dp"/>
    <EditText
        android:id="@+id/editText_twosno"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入要查询的学号"
        android:textColor="#000000"
        android:textSize="25sp" />
    <Button
        android:id="@+id/button_twoquery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="查询"
        android:textSize="25sp"/>
    <TextView
        android:id="@+id/textView_tworesult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示查询结果"
        android:textSize="25sp"
        android:textColor="#000000" />
    <Button
        android:id="@+id/button_twonext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下一页"
        android:textSize="25sp"
        android:layout_gravity="right"
        android:layout_marginTop="30dp" />
</LinearLayout>

SecondActivity.java

package com.example.studentinfotest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
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;
public class SecondActivity extends AppCompatActivity {
    private EditText edit_twosno;
    private Button btn_twoquery, btn_twonext;
    private TextView txt_tworesult;
    private MyDBpenHelper mhelper;//定义一个数据库帮助类对象
    private SQLiteDatabase db;//定义一个操作的数据库的类对象
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        //1.控件初始化
        initView();
        //2."查询"按钮功能的实现
        btnQuety();
        //3."下一页"按钮功能的实现
        btnNext();
    }
    //1.控件初始化
    private void initView() {
        edit_twosno = findViewById(R.id.editText_twosno);
        btn_twoquery = findViewById(R.id.button_twoquery);
        txt_tworesult = findViewById(R.id.textView_tworesult);
        btn_twonext = findViewById(R.id.button_twonext);
        mhelper = new MyDBpenHelper(SecondActivity.this);//实例化数据库帮助类对象
        db = mhelper.getWritableDatabase();//获取数据库的读写权限
    }
    //2."查询"按钮功能的实现
    private void btnQuety() {
        btn_twoquery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //开始查询
                Cursor cursor = db.query("stu_info", new String[]{"sno", "name", "sex", "professional", "department"}, "sno=?",
                        new String[]{edit_twosno.getText().toString()}, null, null, null);
                if(cursor.getCount()!=0){//判断结果集中是否有数据,有:查询成功;无:查询失败
                    Toast.makeText(SecondActivity.this,"查询成功",Toast.LENGTH_SHORT).show();
                    //循环遍历结果集,取出数据,显示出来
                    while (cursor.moveToNext()){
                        @SuppressLint("Range") String mysno=cursor.getString(cursor.getColumnIndex("sno"));
                        @SuppressLint("Range") String myname=cursor.getString(cursor.getColumnIndex("name"));
                        @SuppressLint("Range") String mysex=cursor.getString(cursor.getColumnIndex("sex"));
                        @SuppressLint("Range") String mypro=cursor.getString(cursor.getColumnIndex("professional"));
                        @SuppressLint("Range") String mydep=cursor.getString(cursor.getColumnIndex("department"));

                        txt_tworesult.setText(mysno+"\n"+myname+"\n"+mysex+"\n"+mypro+"\n"+mydep);
                    }

                }else {
                    Toast.makeText(SecondActivity.this, "查询失败", Toast.LENGTH_SHORT).show();
                    txt_tworesult.setText("查询失败");
                }
            }
        });
    }
    private void btnNext() {
        btn_twonext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到第三个界面
                Intent intent=new Intent(SecondActivity.this, DeleteActivity.class);
                startActivity(intent);
            }
        });
    }
}

activity_delete.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".DeleteActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="信息删除页面"
        android:textSize="30sp"
        android:textStyle="bold"
        android:textColor="#000000"
        android:layout_gravity="center"
        android:layout_margin="80dp"/>
    <EditText
        android:id="@+id/editText_foursno"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入要删除的学号"
        android:textSize="25sp"
        android:textColor="#000000" />
    <Button
        android:id="@+id/button_fourdelete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="删除"
        android:textSize="25sp"
        android:textColor="#000000"
        android:layout_gravity="right"/>
    <TextView
        android:id="@+id/textView_fourresult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示删除的结果"
        android:textSize="25sp"
        android:textColor="#000000" />
</LinearLayout>

DeleteActivity.java

package com.example.studentinfotest;
import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
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;
public class DeleteActivity extends AppCompatActivity {
    EditText edit_foursno;
    Button btn_fourdelete;
    MyDBpenHelper mhelper;//定义一个数据库帮助类对象
    SQLiteDatabase db;//定义一个操作的数据库的类对象
    TextView txt_fourresult;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete);
        //1.控件初始化
        initView();
        //2."删除"按钮功能的实现
        btnDelete();
    }
    //1.控件初始化
    private void initView(){
        edit_foursno = findViewById(R.id.editText_foursno);
        btn_fourdelete = findViewById(R.id.button_fourdelete);
        mhelper = new MyDBpenHelper(DeleteActivity.this);//实例化数据库帮助类对象
        db = mhelper.getWritableDatabase();//获取数据库的读写权限
        txt_fourresult = findViewById(R.id.textView_fourresult);
    }
    //2."删除"按钮功能的实现
    private void btnDelete() {
        btn_fourdelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String snoToDelete = edit_foursno.getText().toString();
                Cursor cursor = db.rawQuery("SELECT * FROM stu_info WHERE sno=?", new String[]{snoToDelete});

                // 用于保存删除的数据
                StringBuilder deletedData = new StringBuilder();

                if (cursor.moveToFirst()) {
                    // 遍历结果集,保存要删除的数据
                    do {
                        @SuppressLint("Range") String mysno = cursor.getString(cursor.getColumnIndex("sno"));
                        @SuppressLint("Range") String myname = cursor.getString(cursor.getColumnIndex("name"));
                        @SuppressLint("Range") String mysex = cursor.getString(cursor.getColumnIndex("sex"));
                        @SuppressLint("Range") String mypro = cursor.getString(cursor.getColumnIndex("professional"));
                        @SuppressLint("Range")String mydep = cursor.getString(cursor.getColumnIndex("department"));

                        deletedData.append("学号: ").append(mysno).append("\n");
                        deletedData.append("姓名: ").append(myname).append("\n");
                        deletedData.append("性别: ").append(mysex).append("\n");
                        deletedData.append("专业: ").append(mypro).append("\n");
                        deletedData.append("系所: ").append(mydep).append("\n\n");
                    } while (cursor.moveToNext());
                    // 显示删除前的数据
                    txt_fourresult.setText(deletedData.toString());

                    // 执行删除操作
                    int deleteCount = db.delete("stu_info", "sno=?", new String[]{snoToDelete});

                    if (deleteCount > 0) {
                        Toast.makeText(DeleteActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(DeleteActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    // 没有找到要删除的数据
                    Toast.makeText(DeleteActivity.this, "未找到要删除的数据", Toast.LENGTH_SHORT).show();
                }
                cursor.close();
            }
        });
    }
}

  • 18
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Android Studio是一款非常流行的Android开发工具,可以用来创建、编辑、编译和调试Android应用程序。在Android Studio中,增删项目是非常常见的操作,下面是一些基本的步骤: 1. 增加项目:在Android Studio中,可以使用“New Project”向导来创建新的Android项目。在向导中,您需要选择项目的名称、包名、目标SDK版本、最小SDK版本等信息。 2. 删除项目:要删除项目,可以在Android Studio中选择“File”菜单中的“Close Project”选项。然后,在文件系统中删除项目文件夹。 3. 修改项目:要修改项目,可以在Android Studio中打开项目文件夹,然后编辑项目文件。您可以修改项目的代码、布局、资源等。 4. 项目:要项目,可以使用Android Studio中的搜索功能。您可以搜索项目中的代码、布局、资源等。您还可以使用调试器来看应用程序的运行状态。 总之,Android Studio是一个非常强大的工具,可以帮助您轻松地创建、编辑、编译和调试Android应用程序。无论您是初学者还是有经验的开发人员,都可以使用Android Studio来开发高质量的Android应用程序。 ### 回答2: 如今,Android Studio是最为常用的Android开发工具之一,它凭借着其强大的开发体验以及渐近的开发速度,越来越受广大开发者们的欢迎。在这样的背景下,我们有必要了解一下如何使用Android Studio进行增删项目。 增加项目:要新增一个项目,首先需要打开Android Studio并选择完全新建一个项目,接着要选择合适的安卓设备以及一个合适的模板。进一步来说,开发人员需要确保填写和编辑所有相关详细信息,如数据源、UI组件、图标等。在填写完整个项目细节之后,我们需要构建并运行代码以确保所有内容均已正确操作。 删除项目:删除一个项目的操作非常简单并且可以使用菜单栏里面的选项实现。开发人员只需要选择相应的项目目录,右键选择删除即可。在删除之前,我们需要确保备份了项目,以免不小心删错。 修改项目:开发人员可以在Android Studio中通过编辑代码或更改相关参数来修改一个项目。在许多情况下,要对项目进行更深入的修改,我们需要使用外部文本编辑器来更改资源文件,例如布局文件、字符串文件、活动文件等。 项目:在Android Studio中项目的过程非常简单,只需要使用Ctrl + Shift + F命令打开全局找窗口,然后填写要找的关键字即可。此外,还可以使用顶部菜单栏中的“找”选项来找更具体的类和方法。 总而言之,使用Android Studio进行增删项目是比较容易的。开发人员只需要熟悉这几个基本操作,就可以更有效地创建和管理项目。 ### 回答3: Android Studio是一款强大的应用程序开发工具,可以方便地编写Android应用程序。在开发应用程序时,增删是非常常见的操作。本文将简要介绍如何在Android Studio中实现项目增删。 1. 项目建立与配置 首先,我们需要建立一个项目,并对其进行一些必要的配置。 a. 建立项目 首先,打开Android Studio,并选择建立项目。接着,填写应用程序的包名、项目名称和应用程序名称等信息。 b. 配置Gradle文件 Gradle文件是用于控制项目构建的脚本文件,我们需要配置该文件来引用必要的库文件。在Gradle文件中,我们需要添加以下代码: dependencies { implementation 'com.android.support:appcompat-v7:28.0.0' } 这段代码将引用Android的支持库文件,以确保应用程序能够正常运行。 2. 实现增删 接下来,我们实现项目增删功能。 a. 增加数据 要增加数据,我们需要使用SQLite数据库。首先,在activity_main.xml文件中添加一个EditText控件和一个Button控件,用于输入数据和执行添加操作。 接着,在MainActivity.java文件中实现添加功能,示例代码如下: public void addData(String name, String phone) { SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("name", name); values.put("phone", phone); db.insert("User", null, values); } 在代码中,我们首先获取数据库对象,并创建ContentValues对象。该对象用于存储待添加的数据。接着,我们使用db.insert()方法将数据添加到数据库中。 b. 删除数据 要删除数据,我们需要获取数据库对象,并使用db.delete()方法删除指定的数据。示例代码如下: public void deleteData(String id) { SQLiteDatabase db = dbHelper.getWritableDatabase(); db.delete("User", "id = ?", new String[]{id}); } 在代码中,我们使用db.delete()方法删除指定的数据。其中,“id = ?”是删除条件,用于指定删除哪些数据。新建一个String数组,该数组中的每一个元素对应删除条件中的一个“?”符号。 c. 修改数据 修改数据与删除数据的操作类似,我们首先获取数据库对象,然后使用db.update()方法修改指定的数据。示例代码如下: public void updateData(String id, String name, String phone) { SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("name", name); values.put("phone", phone); db.update("User", values, "id = ?", new String[]{id}); } 在代码中,我们首先创建ContentValues对象并存储修改后的数据,然后使用db.update()方法修改指定数据。 d. 询数据 询数据也是非常常见的操作。我们可以使用db.query()方法询指定的数据。示例代码如下: public Cursor queryData(String key) { SQLiteDatabase db = dbHelper.getWritableDatabase(); return db.query("User", null, "name like ?", new String[]{"%" + key + "%"}, null, null, null); } 在代码中,我们使用db.query()方法询指定的数据。其中,“name like ?”是询条件,用于指定询哪些数据。新建一个String数组,该数组中的每一个元素对应询条件中的一个“?”符号。 以上就是在Android Studio中实现项目增删的基本操作,希望对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值