DB
package com.example.myapplication;
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.util.Log;
import androidx.annotation.Nullable;
import java.util.ArrayList;
public class DB extends SQLiteOpenHelper {
public DB(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
private SQLiteDatabase dp;
public DB(Context context){
super(context,"TEST1.dp",null,1);
dp=this.getWritableDatabase();
}
public boolean add(String name,String psw) {
ContentValues values = new ContentValues();
values.put("name", name);
values.put("psw", psw);
long i=dp.insert("users",null,values);
if(i>0){
Log.d("","插入成功");
return true;
}
return true;
}
public boolean del(String name, String p){
long i=dp.delete("users","name=?",new String[]{name});
if(i>0){
Log.d("","删除成功");
return true;
}
return true;
}
public boolean change(String name,String NewPsw){
ContentValues values=new ContentValues();
values.put("psw",NewPsw);
long i=dp.update("users",values,"name=?",new String[]{name});
if(i>0){
Log.d("","修改成功");
return true;
}
return true;
}
public ArrayList getAll(){
ArrayList array=new ArrayList();
Cursor cursor=dp.query("users",null,null,null,null,null,null);
while (cursor.moveToNext()){
@SuppressLint("Range") String name=cursor.getString(cursor.getColumnIndex("name"));
@SuppressLint("Range") String psw=cursor.getString(cursor.getColumnIndex("psw"));
User u=new User(name,psw);
array.add(u);
}
return array;
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql="create table users (name primary key,psw text not null) ";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
MainActivity
package com.example.myapplication;
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.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import java.util.ArrayList;
import java.util.Objects;
public class MainActivity extends AppCompatActivity {
EditText name,psw;
Button add,del,change,seeAll;
TextView content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
name=findViewById(R.id.name);
psw=findViewById(R.id.psw);
add=findViewById(R.id.add);
del=findViewById(R.id.del);
change=findViewById(R.id.change);
seeAll=findViewById(R.id.seeAll);
content=findViewById(R.id.content);
DB db=new DB(this);
//添加
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String n=name.getText().toString();
String p=psw.getText().toString();
if(db.add(n,p)){
Toast.makeText(MainActivity.this,"添加成功",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this,"添加失败",Toast.LENGTH_SHORT).show();
}
}
});
//修改 通过用户账号name修改密码psw
change.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String n=name.getText().toString();
String p=psw.getText().toString();
if(db.change(n,p)){
Toast.makeText(MainActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this,"修改失败",Toast.LENGTH_SHORT).show();
}
}
});
//删除用户
del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String n=name.getText().toString();
String p=psw.getText().toString();
if(db.del(n,p)){
Toast.makeText(MainActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this,"删除失败",Toast.LENGTH_SHORT).show();
}
}
});
//查询所有人
seeAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
content.setText("");//查看所有用户,所以需要先清空
ArrayList a=db.getAll();
for(Object b:a){
String n=content.getText().toString();
String str=n+((User)b).toString()+"\n";
content.setText(str);
}
}
});
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
}
}
User
package com.example.myapplication;
public class User {
String name;
String psw;
public User(String name, String psw) {
this.name = name;
this.psw = psw;
}
@Override
public String toString() {
return name + ',' +psw ;
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/htc"
android:label="阿磊增删改查"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
标签:String,日报,3.11,psw,public,import,android,软甲,name From: https://www.cnblogs.com/galileo9527/p/18067169