2023年2.28日软件日报

今天又是无聊的一天,下午上了python课,python没听太懂,很难阿。没什么仔细听。然后下午回来吃会儿零食,睡了一觉。然后晚上写代码,代码时间大概2小时吧,写了安卓数据库增删改查。

代码如下:

SQLiteHelperActivity2 .java
  1 package com.example.sql;
  2 
  3 import androidx.appcompat.app.AppCompatActivity;
  4 
  5 import android.os.Bundle;
  6 import android.util.Log;
  7 import android.view.View;
  8 import android.widget.CheckBox;
  9 import android.widget.EditText;
 10 import android.widget.ImageView;
 11 
 12 import com.example.sql.database.UserDBHelper0;
 13 import com.example.sql.enity.User;
 14 import com.example.sql.util.ToastUtil;
 15 
 16 import java.util.List;
 17 
 18 public class SQLiteHelperActivity2 extends AppCompatActivity implements View.OnClickListener {
 19     private EditText et_name;
 20     private EditText et_age;
 21     private EditText et_height;
 22     private EditText et_weight;
 23     private CheckBox ck_married;
 24     private  UserDBHelper0 mHelper;
 25 
 26     @Override
 27     protected void onCreate(Bundle savedInstanceState) {
 28         super.onCreate(savedInstanceState);
 29         setContentView(R.layout.activity_sqlite_helper2);
 30         et_name = findViewById(R.id.et_name);
 31         et_age = findViewById(R.id.et_age);
 32         et_height = findViewById(R.id.et_height);
 33         et_weight = findViewById(R.id.et_weight);
 34         ck_married = findViewById(R.id.ck_married);
 35 
 36         findViewById(R.id.btn_save).setOnClickListener(this);
 37         findViewById(R.id.btn_delete).setOnClickListener(this);
 38         findViewById(R.id.btn_update).setOnClickListener(this);
 39         findViewById(R.id.btn_query).setOnClickListener(this);
 40     }
 41 
 42     @Override
 43     protected void onStart() {
 44         super.onStart();
 45         mHelper = UserDBHelper0.getInstance(this);
 46         //打开数据库读和写
 47         mHelper.openWriteLink();
 48         mHelper.openReadLink();
 49     }
 50 
 51     @Override
 52     protected void onStop() {
 53         super.onStop();
 54         mHelper.closeLInk();
 55     }
 56 
 57     @Override
 58     public void onClick(View v) {
 59         String name = et_name.getText().toString();
 60         String age = et_age.getText().toString();
 61         String height = et_height.getText().toString();
 62         String weight = et_weight.getText().toString();
 63         User user = null;
 64         switch (v.getId()) {
 65             case R.id.btn_save:
 66                 // 以下声明一个用户信息对象,并填写它的各字段值
 67                 user = new User(name,
 68                         Integer.parseInt(age),
 69                         Long.parseLong(height),
 70                         Float.parseFloat(weight),
 71                         ck_married.isChecked());
 72                 if (mHelper.insert(user) > 0) {
 73                     ToastUtil.show(this, "添加成功");
 74                 }
 75                 break;
 76             case R.id.btn_delete:
 77                 if (mHelper.deleteByName(name) > 0) {
 78                     ToastUtil.show(this, "删除成功");
 79                 }
 80                 break;
 81             case R.id.btn_update:
 82                 user = new User(name,
 83                         Integer.parseInt(age),
 84                         Long.parseLong(height),
 85                         Float.parseFloat(weight),
 86                         ck_married.isChecked());
 87                 if (mHelper.updata(user) > 0) {
 88                     ToastUtil.show(this, "修改成功");
 89                 }
 90                 break;
 91             case R.id.btn_query:
 92                 List<User> list = mHelper.queryAll();
 93                 //List<User> list = mHelper.queryByName(name);
 94                 for (User u : list) {
 95                     Log.d("ning", u.toString());
 96                 }
 97                 break;
 98 
 99         }
100 
101     }
102 }
UserDBHelper0.java
package com.example.sql.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.sql.enity.User;

import java.util.LinkedList;
import java.util.List;

public class UserDBHelper0 extends SQLiteOpenHelper {
   private  static final String DB_Name="user.db";
   private  static final String Table="user_info";
   private static  UserDBHelper0 mHelper =null;
   private static final int DB_VERSION=1;
   private SQLiteDatabase mRDB =null;
   private SQLiteDatabase mWDB=null;
    private UserDBHelper0(Context context)
    {
        super(context,DB_Name,null,DB_VERSION);
    }

    //创建数据库,执行语句
    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql="CREATE TABLE IF NOT EXISTS " +Table+" ("+
                "_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
                "name VARCHAR NOT NULL," +
                "age INTEGER NOT NULL," +
                "height LONG NOT NULL," +
                "weight FLOAT NOT NULL," +
                "married INTEGER NOT NULL);";

        db.execSQL(sql);
    }
    public static UserDBHelper0 getInstance(Context context) {
        if (mHelper == null) {
            mHelper = new UserDBHelper0(context);
        }
        return mHelper;
    }
    public SQLiteDatabase openReadLink()
    {
        if(mRDB==null||!mRDB.isOpen())
        {
            mRDB =mHelper.getReadableDatabase();
        }
        return  mRDB;
    }

    public SQLiteDatabase openWriteLink() {
        if(mWDB==null ||!mWDB.isOpen())
        {
            mWDB=mHelper.getReadableDatabase();
        }
        return mWDB;
    }
    //关闭数据库
    public void closeLInk()
    {
        if(mRDB!=null &&mRDB.isOpen())
        {
            mRDB.close();;
            mRDB=null;
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
    public  long insert(User user)
    {
        ContentValues values=new ContentValues();
        values.put("name",user.name);
        values.put("age",user.age);
        values.put("height",user.height);
        values.put("weight",user.weight);
        values.put("married",user.married);
       return mWDB.insert(Table,null,values);
    }
    public long deleteByName(String name)
    {
        //删除所有
        //mWDB.delete(Table,"1=1",null);
       return mWDB.delete(Table,"name=?",new String[]{name});
    }
    public long updata(User user)
    {
        ContentValues values=new ContentValues();
        return mWDB.update(Table,values,"name=?",new String[]{user.name});
    }
    public List<User> queryAll()
    {
        List<User> list=new LinkedList<>();
        Cursor query = mRDB.query(Table, null, null, null, null, null, null);
        while(query.moveToNext())
        {
            User user=new User();
            user.id=query.getInt(0);
            user.name=query.getString(1);
            user.age=query.getInt(2);  user.height=query.getLong(3);
            user.weight=query.getFloat(4);
            user.married=(query.getInt(5)==0)?false:true;
            list.add(user);

        }

        return list;
    }
    public List<User> queryByname(String name)
    {
        List<User> list=new LinkedList<>();
        Cursor query = mRDB.query(Table, null, "name=?", new String[]{name}, null, null, null);
        while(query.moveToNext())
        {
            User user=new User();
            user.id=query.getInt(0);
            user.name=query.getString(1);
            user.age=query.getInt(2);  user.height=query.getLong(3);
            user.weight=query.getFloat(4);
            user.married=(query.getInt(5)==0)?false:true;
            list.add(user);

        }

        return list;
    }


}
ToastUtil.java
package com.example.sql.util;

import android.content.Context;
import android.widget.Toast;

public class ToastUtil {

    public static void show(Context ctx, String desc) {
        Toast.makeText(ctx, desc, Toast.LENGTH_SHORT).show();
    }
}

User.java

package com.example.sql.enity;
public class User {
  public int id;
  public String name;
  public  int age;
  public long height;
  public float weight;
  public  boolean married;
  public User()
  {

  }
    public User(String name, int age, long height, float weight, boolean married) {
        this.name = name;
        this.age = age;
        this.height = height;
        this.weight = weight;
        this.married = married;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", height=" + height +
                ", weight=" + weight +
                ", married=" + married +
                '}';
    }
}

界面:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="姓名:"
            android:textColor="@color/black"
            android:textSize="17sp" />

        <EditText
            android:id="@+id/et_name"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:hint="请输入姓名"
            android:inputType="text"
            android:maxLength="12"
            android:textColor="@color/black"
            android:textSize="17sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_age"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="年龄:"
            android:textColor="@color/black"
            android:textSize="17sp" />

        <EditText
            android:id="@+id/et_age"
            android:layout_width="0dp"
            android:layout_height="match_parent"

            android:layout_weight="1"

            android:hint="请输入年龄"
            android:inputType="number"
            android:maxLength="2"
            android:textColor="@color/black"
            android:textSize="17sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_height"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="身高:"
            android:textColor="@color/black"
            android:textSize="17sp" />

        <EditText
            android:id="@+id/et_height"
            android:layout_width="0dp"
            android:layout_height="match_parent"

            android:layout_weight="1"

            android:hint="请输入身高"
            android:inputType="number"
            android:maxLength="3"
            android:textColor="@color/black"
            android:textSize="17sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_weight"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="体重:"
            android:textColor="@color/black"
            android:textSize="17sp" />

        <EditText
            android:id="@+id/et_weight"
            android:layout_width="0dp"
            android:layout_height="match_parent"

            android:layout_weight="1"

            android:hint="请输入体重"
            android:inputType="numberDecimal"
            android:maxLength="5"
            android:textColor="@color/black"
            android:textSize="17sp" />
    </LinearLayout>

    <CheckBox
        android:id="@+id/ck_married"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:gravity="center"
        android:text="已婚"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <Button
        android:id="@+id/btn_save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="添加"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <Button
        android:id="@+id/btn_delete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="删除"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <Button
        android:id="@+id/btn_update"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="修改"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <Button
        android:id="@+id/btn_query"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="查询"
        android:textColor="@color/black"
        android:textSize="17sp" />

</LinearLayout>

 

posted @ 2023-02-28 21:55  阿飞藏泪  阅读(5)  评论(0编辑  收藏  举报
1 2 3
4