本篇博客来介绍安卓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())标签:String,安卓,db,软件工程,studio,et,import,password,id From: https://www.cnblogs.com/jiacheng-712/p/17185808.html
}
语句实现了通过调用switch语句进行增删改查,每种方法都要两种形式