工程文件
https://download.csdn.net/download/qq_45688164/51971151
前端框架
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:padding="5dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="用户名:"
android:textSize="30sp" />
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_toRightOf="@id/tv_username"
android:background="@drawable/shape_editext"
android:minLines="2" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/et_username"
android:layout_alignLeft="@id/et_username"
android:layout_marginTop="10dp"
android:background="@drawable/shape_editext"
android:inputType="textPassword"
android:minLines="2"
android:textSize="30sp" />
<TextView
android:id="@+id/tv_choiceB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/et_password"
android:text="密 码:"
android:textSize="30sp" />
<EditText
android:id="@+id/et_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/et_password"
android:layout_alignLeft="@id/et_password"
android:layout_marginTop="10dp"
android:background="@drawable/shape_editext"
android:minLines="2" />
<TextView
android:id="@+id/tv_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/et_age"
android:text="年 龄:"
android:textSize="30sp" />
<Button
android:id="@+id/bt_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_age"
android:layout_alignTop="@id/bt_query"
android:text="保存"
android:textSize="23sp" />
<Button
android:id="@+id/bt_query"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/et_age"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="@id/bt_save"
android:text="查询"
android:textSize="23sp" />
<Button
android:id="@+id/bt_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/et_age"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="@id/bt_query"
android:text="修改"
android:textSize="23sp" />
<Button
android:id="@+id/bt_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/et_age"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="@id/bt_update"
android:text="删除"
android:textSize="23sp" />
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/bt_save"
android:text=""
android:scrollbars="vertical"
android:singleLine="false"
android:textSize="20sp" />
</RelativeLayout>
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentResolver;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private UsersDao dao;
private User user;
private EditText et_username, et_password, et_age;
private Button bt_save, bt_query, bt_update, bt_delete;
private ArrayList<User> datas;
private ArrayList datas_jiexi;
private TextView tv_show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建数据库
init();
dao = new UsersDao(this);
}
public void init() {
et_username = findViewById(R.id.et_username);
et_password = findViewById(R.id.et_password);
et_age = findViewById(R.id.et_age);
bt_save = findViewById(R.id.bt_save);
bt_query = findViewById(R.id.bt_query);
bt_update = findViewById(R.id.bt_update);
bt_delete = findViewById(R.id.bt_delete);
tv_show=findViewById(R.id.tv_show);
tv_show.setMovementMethod(ScrollingMovementMethod.getInstance());
bt_save.setOnClickListener(this);
bt_query.setOnClickListener(this);
bt_update.setOnClickListener(this);
bt_delete.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_save: {
//保存插入数据
user = new User(et_username.getText().toString(), et_password.getText().toString(), Integer.parseInt(et_age.getText().toString()));
mass(dao.insert(user));
break;
}
case R.id.bt_query: {
//查询数据
datas=dao.queryAll();
String us="";
for (int i=0;i<datas.size();i++) {
us+="\nid:"+datas.get(i).getId()+" name:"+datas.get(i).getName()+" pw:"+datas.get(i).getPasword()+" age:"+datas.get(i).getAge()+"\n";
}
tv_show.setText(us);
break;
}
case R.id.bt_update: {
//修改数据
user = new User(et_username.getText().toString(), et_password.getText().toString(), Integer.parseInt(et_age.getText().toString()));
mass(dao.updateUser(user));
break;
}
case R.id.bt_delete: {
//删除数据
mass(dao.deleteUser(et_username.getText().toString()));
break;
}
default:
break;
}
}
public void mass(int flag) {
if (flag != -1) {
Toast.makeText(this, "操作成功" + flag, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "网络异常", Toast.LENGTH_LONG).show();
}
}
}
用户表工具类
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.Toast;
import java.util.ArrayList;
public class UsersDao {
SQLiteDatabase db;
MySQLiteOpenHaper sqLite;
Context context;
public UsersDao(Context context) {
sqLite = new MySQLiteOpenHaper(context, "user.db", null, 1);
this.context = context;
}
//增添数据
public int insert(User user) {
int flag = -1;
db = sqLite.getWritableDatabase();
ContentValues values = new ContentValues();
// values.put("userno",user.getId());
values.put("username", user.getName());
values.put("userpassword", user.getPasword());
values.put("userage", user.getAge());
flag = (int) db.insert("user", null, values);
db.close();
return flag;
}
//删除数据
public int deleteUser(String username) {
int flag = -1;
db = sqLite.getWritableDatabase();
flag = (int) db.delete("user", "username=?", new String[]{username + ""});
db.close();
return flag;
}
//更新数据
public int updateUser(User user) {
db = sqLite.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("userpassword", user.getPasword());
values.put("userage", user.getAge());
int number = db.update("user", values, "username = ?",
new String[]{user.getName()});
db.close();
return number;
}
//查询数据
public ArrayList queryAll() {
ArrayList list = new ArrayList();
db = sqLite.getWritableDatabase();
Cursor cursor = db.query("user", null, null, null, null,
null, null);
list = convertFromCursor(cursor);
return list;
}
//将查询数据组织为集合
private ArrayList convertFromCursor(Cursor cursor) {
ArrayList<User> list = new ArrayList<User>();
if (cursor != null && cursor.moveToFirst()) {
//通过游标遍历整个查询结果集
do {
int id = cursor.getInt(cursor.getColumnIndex("userid"));
String username = cursor.getString(cursor.getColumnIndex("username"));
String password = cursor.getString(cursor.getColumnIndex("userpassword"));
int age = cursor.getInt(cursor.getColumnIndex("userage"));
User user = new User(id, username, password, age);
list.add(user);
} while (cursor.moveToNext());
}
return list;
}
}
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
import androidx.annotation.Nullable;
public class MySQLiteOpenHaper extends SQLiteOpenHelper {
Context context;
public MySQLiteOpenHaper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
this.context=context;
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table user ("
+ "userid integer primary key autoincrement, "
+ "username text, "
+ "userpassword text, "
+ "userage integer)";
db.execSQL(sql);
Toast.makeText(context,"onCreate 以运行",Toast.LENGTH_LONG).show();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public class User {
int id;
String name;
String pasword;
int age;
public User(int id, String name, String pasword, int age) {
this.id = id;
this.name = name;
this.pasword = pasword;
this.age = age;
}
public User(String name, String pasword, int age) {
this.id = id;
this.name = name;
this.pasword = pasword;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPasword() {
return pasword;
}
public void setPasword(String pasword) {
this.pasword = pasword;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pasword='" + pasword + '\'' +
", age=" + age +
'}';
}
}