首页 > 其他分享 >软件工程日报十二——安卓studio数据的删除与修改

软件工程日报十二——安卓studio数据的删除与修改

时间:2023-03-08 19:11:23浏览次数:39  
标签:String 安卓 db 软件工程 studio et import password id

本篇博客来介绍安卓studio的数据删除与修改

源码如下:

Mainactivity

package com.example.sqlitetest;

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

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button generateBtn,addBtn,deleteBtn,updateBtn,selectBtn;
    private EditText et_username,et_password,et_selection;
    private TextView showInfo;
    private MyDbHelper myDbhelper;
    private SQLiteDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myDbhelper=new MyDbHelper(MainActivity.this,"MyDatabase.db",null,666);
        initView();
        generateBtn.setOnClickListener(this);
        addBtn.setOnClickListener(this);
        selectBtn.setOnClickListener(this);
        deleteBtn.setOnClickListener(this);
        updateBtn.setOnClickListener(this);
    }

    @SuppressLint("WrongViewCast")
    private void initView() {
       generateBtn=findViewById(R.id.generate);
        addBtn=findViewById(R.id.add);
        deleteBtn=findViewById(R.id.delete);
        updateBtn=findViewById(R.id.update);
        selectBtn=findViewById(R.id.select);
        et_username=findViewById(R.id.username);
         et_password=findViewById(R.id.password);
       showInfo=findViewById(R.id.showInfo);
        et_selection=findViewById(R.id.selection);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId())
        {

            case R.id.add:
                //通过帮助类获取数据对象
                db=myDbhelper.getWritableDatabase();
                String username=et_username.getText().toString();
                String password=et_password.getText().toString();

               /* //创建一个ContentValues对象,用于存储记录的字段值,以键值对的方式储存,“键”对应就是字段名,“值”对应就是某个字段具体的值
                ContentValues contentValues=new ContentValues();

                contentValues.put("userName",username);
                contentValues.put("password",password);
                db.insert("user",null,contentValues);*/
                db.execSQL("insert into user(userName,password) values(?,?)",new Object[]{username,password});
             db.close();
                break;
            case R.id.select://普通查询
                db=myDbhelper.getWritableDatabase();
                Cursor cursor=db.query("user",new String[]{"username","password"},null,null,null,null,null,null);
                cursor.moveToFirst();
                showInfo.setText("用户名"+cursor.getString(0)+",密码"+cursor.getString(1));
                while(cursor.moveToNext())
                {
                    showInfo.append("\n"+"用户名"+cursor.getString(0)+",密码"+cursor.getString(1));
                }
                cursor.close();
                db.close();
                break;
            case R.id.generate://条件查询
                db=myDbhelper.getWritableDatabase();
                String selection=et_selection.getText().toString();
               // Cursor cursor1=db.query("user",new String[]{"username","password"},"username=?",new String[]{selection},null,null,null,null);
                Cursor cursor1=db.rawQuery("select userName,password from user where userName=?",new String[]{selection});
                showInfo.setText("查询结果如下");
                while(cursor1.moveToNext())
                {
                    showInfo.append("\n"+"用户名"+cursor1.getString(0)+",密码"+cursor1.getString(1));
                }
                cursor1.close();
                db.close();
                break;
            case R.id.delete:
                db=myDbhelper.getWritableDatabase();
                String selection1=et_selection.getText().toString();
                /*int i=db.delete("user","username=?",new String[]{selection1});
                if(i>0)
                {
                    Toast.makeText(MainActivity.this,"删除成功,删除了"+i+"条",Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(MainActivity.this,"删除失败",Toast.LENGTH_SHORT).show();
                }*/
                db.execSQL("delete from user where username=?",new Object[]{selection1});

                db.close();
                break;
                case R.id.update:
                    db=myDbhelper.getWritableDatabase();
                    String username1=et_username.getText().toString();
                    String password1=et_password.getText().toString();
                    String selection2=et_selection.getText().toString();
                    db.execSQL("update user set userName=?,password=? where userName=?",new Object[]{username1,password1,selection2});
                    /*
                    ContentValues contentValues=new ContentValues();
                    contentValues .put("userName",username1);
                    contentValues .put("password",password1);
                    db.update("user",contentValues,"userName=?",new String[]{selection2});

                     */
                    db.close();
                   break;

        }
    }

    //格式化快捷键 ctrl+alt+l
    //数据库帮助类
    class MyDbHelper extends SQLiteOpenHelper{
//构造器的作用,参数含义:上下文,数据库名称,结果集工厂,版本号
        public MyDbHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
        }
//数据库初始化,用来建表
        @Override
        public void onCreate(SQLiteDatabase db) {
db.execSQL("create table user(user_id integer primary key autoincrement,userName varchar(10),password varchar(10))");
        }
//升级方法
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        }
    }
}

在源码里面包含了安卓studio内数据的增删改查内容,通过

setOnClickListener(this)和

public void onClick(View v) {
        switch(v.getId())
}
语句实现了通过调用switch语句进行增删改查,每种方法都要两种形式

标签:String,安卓,db,软件工程,studio,et,import,password,id
From: https://www.cnblogs.com/jiacheng-712/p/17185808.html

相关文章

  • 【GiraKoo】Android Studio控制台乱码
    【GiraKoo】AndroidStudio控制台乱码启动AndroidStudio进行编译时,可能会遇到控制台出现异常的乱码。本文介绍该情况的解决方案。����:δ������쳣����Exception;���������в���������Ա��׳� mlocatio......
  • VisualStudio(2022)- 打包项目文件为.exe安装包
    目录​​前言​​:​​一、安装扩展​​​​二、制作安装包(setup文件)​​​​2.1、添加setup项目​​​​2.2、配置setup项目​​​​2.3、添加项目文件到setup项目中​​​......
  • 2023.3.8——软件工程日报
    所花时间(包括上课):8h代码量(行):0行博客量(篇):1篇今天,上午学习英语和数据库,下午学习python和数学建模。我了解到的知识点:1.数学建模的一些知识;2.连接sqlite的数据库的插件,......
  • 2023年3月7日(软件工程日报)
    今天开始分两方面,一方面学习安卓新知识另一方面每天学习javaweb相关知识,主要感觉自己上学期javaweb只学习了一些皮毛,需要深入理解一下。今天主要内容为相关控件的内容,ui......
  • 软件工程学习第十三天
    今天我又格外拿出了半小时继续学习css,今天的内容是css的display和Visibility。display可以设置一个元素应如何显示,而Visibility则可以指定一个元素显示还是隐藏。不过隐藏......
  • 软件工程日报——第十二天
    Androidstudio的数据库的增删改查功能实现创建一个DatabaseHelper用来实现操作importandroid.content.Context;importandroid.database.sqlite.SQLiteDatabase;import......
  • 软件工程日报十一——安卓studio的数据查询
    上一篇博客实现了安卓studio内数据的添加,这篇博客来实现数据的查询。mainactivity_main.xml文件<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android=......
  • 如何在 OSX 上从命令行打开 Visual Studio Code?
    文档提到了一个名为的可执行文件,但我不确定在哪里可以找到它,所以我可以将它放在我的路径上。code我从VSCode站点下载的zip不包含任何此类可执行文件。(我能够运行......
  • 关于Android Studio的Activity的页面跳转完成
    第一种方式Intentintent=newIntent();intent.setClass(this,MainActivity3.class);startActivity(intent);第二种方式Intentintent=newIntent();intent.setClas......
  • 软件工程学习第十二天
    今天我们上课讲解了如何规范代码,规范代码十分重要。代码是需要维护的,无论是自己维护,还是其他人维护,都需要阅读代码。符合规范的代码,能减少理解成本。其次,代码规范最大的目......