首页 > 其他分享 >3月11号(工程日志第五天)

3月11号(工程日志第五天)

时间:2024-03-11 21:34:34浏览次数:23  
标签:11 第五天 id KEY import 日志 null public android

所学时间:4小时

代码行数:413行

博客园数:1篇

所学知识:今天将学习了安卓开发对于数据库sqlite的增删改查操作,并学会了部分的试图修改操作。

MainActivity

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    EditText e_xm,e_nl,e_sg,e_id;
    TextView t_1;
    Button b_add,b_allsee,b_clearsee,b_alldel,b_delid,b_seeid,b_updid;
    DBAdapter dbAdapter;
    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        e_xm=findViewById(R.id.e_xm);
        e_nl=findViewById(R.id.e_nl);
        e_sg=findViewById(R.id.e_sg);
        b_add=findViewById(R.id.b_add);
        b_allsee=findViewById(R.id.b_allsee);
        b_clearsee=findViewById(R.id.b_clearall);
        b_alldel=findViewById(R.id.b_delall);
        b_delid=findViewById(R.id.b_delid);
        b_seeid=findViewById(R.id.b_seeid);
        b_updid=findViewById(R.id.b_updid);
        e_id=findViewById(R.id.e_id);
        t_1=findViewById(R.id.t_1);
        dbAdapter=new DBAdapter(this);
        dbAdapter.open();

        @SuppressLint({"MissingInflatedId", "LocalSuppress"}) Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,MainActivity2.class);
                startActivity(intent);
            }
        });


        b_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                People t=new People();
                t.Name=e_xm.getText().toString();
                t.Banji=e_nl.getText().toString();
                t.Xuehao=e_sg.getText().toString();
                long colunm=dbAdapter.insert(t);
                if (colunm == -1 ){
                    t_1.setText("添加过程错误!");
                } else {
                    t_1.setText("成功添加数据,ID:"+String.valueOf(colunm));
                }
            }
        });

        b_allsee.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                People [] peoples =dbAdapter.queryAllData();
                if (peoples == null){
                    t_1.setText("数据库中没有数据");
                    return;
                }
                String t="数据库:\n";
                for(int i=0;i<peoples.length;++i){
                    t+=peoples[i].toString()+"\n";
                }
                t_1.setText(t);
            }
        });

        b_clearsee.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                t_1.setText("");
            }
        });

        b_alldel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dbAdapter.deleteAllData();
                t_1.setText("已删除所有数据!");
            }
        });

        b_delid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int id=Integer.parseInt(e_id.getText().toString());
                long result=dbAdapter.deleteOneData(id);
                String msg = "删除ID为"+e_id.getText().toString()+"的数据" + (result>0?"成功":"失败");
                t_1.setText(msg);
            }
        });

        b_seeid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int id=Integer.parseInt(e_id.getText().toString());
                People people[]=dbAdapter.queryOneData(id);
                if(people==null){
                    t_1.setText("Id为"+id+"的记录不存在!");
                }
                else{
                    t_1.setText("查询成功:\n"+people[0].toString());
                }
            }
        });

        b_updid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int id=Integer.parseInt(e_id.getText().toString());
                People t=new People();
                t.Name=e_xm.getText().toString();
                t.Banji=e_nl.getText().toString();
                t.Xuehao=e_sg.getText().toString();
                long n=dbAdapter.updateOneData(id,t);
                if (n<0){
                    t_1.setText("更新过程错误!");
                } else {
                    t_1.setText("成功更新数据,"+String.valueOf(n)+"条");
                }
            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
        dbAdapter.close();
    }
}

DBAdapter

 

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.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class DBAdapter {

    private static final String DB_NAME = "student.db";
    private static final String DB_TABLE = "peopleinfo";
    private static final int DB_VERSION = 1;

    public static final String KEY_ID = "_id";
    public static final String KEY_NAME = "name";
    public static final String KEY_BANJI = "banji";
    public static final String KEY_XUEHAO = "xuehao";

    private SQLiteDatabase db;
    private final Context context;
    private DBOpenHelper dbOpenHelper;

    public DBAdapter(Context _context) {
        context = _context;
    }

    public void close() {
        if(db !=null)
        {
            db.close();
            db=null;
        }
    }

    public void open() throws SQLiteException {
        dbOpenHelper = new DBOpenHelper(context, DB_NAME, null, DB_VERSION);
        try {
            db = dbOpenHelper.getWritableDatabase();
        }
        catch (SQLiteException ex) {
            db = dbOpenHelper.getReadableDatabase();
        }
    }


    public long insert(People people) {
        ContentValues newValues = new ContentValues();
        newValues.put(KEY_NAME, people.Name);
        newValues.put(KEY_BANJI, people.Banji);
        newValues.put(KEY_XUEHAO, people.Xuehao);

        return db.insert(DB_TABLE, null, newValues);
    }


    public People[] queryAllData() {
        Cursor results =  db.query(DB_TABLE, new String[] { KEY_ID, KEY_NAME, KEY_BANJI, KEY_XUEHAO},
                null, null, null, null, null);
        return ConvertToPeople(results);
    }

    public People[] queryOneData(long id) {
        Cursor results =  db.query(DB_TABLE, new String[] { KEY_ID, KEY_NAME, KEY_BANJI, KEY_XUEHAO},
                KEY_ID + "=" + id, null, null, null, null);
        return ConvertToPeople(results);
    }

    @SuppressLint("Range")
    private People[] ConvertToPeople(Cursor cursor){
        int resultCounts = cursor.getCount();
        if (resultCounts == 0 || !cursor.moveToFirst()){
            return null;
        }
        People[] peoples = new People[resultCounts];
        for (int i = 0 ; i<resultCounts; i++){
            peoples[i] = new People();
            peoples[i].ID = cursor.getInt(0);
            peoples[i].Name = cursor.getString(cursor.getColumnIndex(KEY_NAME));
            peoples[i].Banji = cursor.getString(cursor.getColumnIndex(KEY_BANJI));
            peoples[i].Xuehao = cursor.getString(cursor.getColumnIndex(KEY_XUEHAO));
            cursor.moveToNext();
        }
        return peoples;
    }

    public long deleteAllData() {
        return db.delete(DB_TABLE, null, null);
    }

    public long deleteOneData(long id) {
        return db.delete(DB_TABLE,  KEY_ID + "=" + id, null);
    }

    public long updateOneData(long id , People people){
        ContentValues updateValues = new ContentValues();
        updateValues.put(KEY_NAME, people.Name);
        updateValues.put(KEY_BANJI, people.Banji);
        updateValues.put(KEY_XUEHAO, people.Xuehao);

        return db.update(DB_TABLE, updateValues,  KEY_ID + "=" + id, null);
    }

    private static class DBOpenHelper extends SQLiteOpenHelper {

        public DBOpenHelper(Context context, String name, CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

        private static final String DB_CREATE = "create table " +
                DB_TABLE + " (" + KEY_ID + " integer primary key autoincrement, " +
                KEY_NAME+ " text not null, " + KEY_BANJI+ " text not null," + KEY_XUEHAO + " text not null);";

        @Override
        public void onCreate(SQLiteDatabase _db) {
            _db.execSQL(DB_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) {
            _db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
            onCreate(_db);
        }
    }
}

 

People

package com.example.myapplication;

public class People {
    public int ID = -1;
    public String Name;
    public String Banji;
    public String Xuehao;

    @Override
    public String toString(){
        String result = "";
        result += "ID:" + this.ID + ",";
        result += "姓名:" + this.Name + ",";
        result += "班级:" + this.Banji + ", ";
        result += "学号:" + this.Xuehao;
        return result;
    }
}

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/teal_200"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:textColor="@color/material_dynamic_neutral0"
            android:text="姓名:"/>

        <EditText
            android:id="@+id/e_xm"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:textColor="@color/material_dynamic_neutral0"
            android:text="班级:"/>

        <EditText
            android:id="@+id/e_nl"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:textColor="@color/material_dynamic_neutral0"
            android:text="学号:"/>

        <EditText
            android:id="@+id/e_sg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            />
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        >
        <Button
            android:id="@+id/b_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAlignment="center"
            android:layout_weight="1"
            android:textSize="16dp"
            android:text="添加数据"/>

        <Button
            android:id="@+id/b_allsee"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAlignment="center"
            android:layout_weight="1"
            android:textSize="16dp"
            android:text="全部显示"/>
        <Button
            android:id="@+id/b_clearall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAlignment="center"
            android:layout_weight="1"
            android:textSize="16dp"
            android:text="清除显示"/>
        <Button
            android:id="@+id/b_delall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAlignment="center"
            android:layout_weight="1"
            android:textSize="16dp"
            android:text="全部删除"/>
    </LinearLayout>

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:textColor="@color/material_dynamic_neutral0"
            android:text="ID:" />

        <EditText
            android:id="@+id/e_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:singleLine="true" />

        <Button
            android:id="@+id/b_delid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textAlignment="center"
            android:text="ID删除"/>
        <Button
            android:id="@+id/b_seeid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textAlignment="center"
            android:text="ID查询"/>
        <Button
            android:id="@+id/b_updid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textAlignment="center"
            android:text="ID更新"/>
    </LinearLayout>

    <TextView
        android:id="@+id/t_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="跳转">

    </Button>

</LinearLayout>

 

标签:11,第五天,id,KEY,import,日志,null,public,android
From: https://www.cnblogs.com/jiajiayu/p/18067119

相关文章

  • abc311D 走迷宫之不撞南墙不回头
    有个大小为n*m的二维图,.为空地,#为障碍,最外层一圈固定为障碍,起点(2,2)固定为空地,每次可以沿上下左右其中一个方向走,直到碰见障碍才能转向。问最多可以走过多少个空地?初始时方向任意,可以走多次。bfs模拟,由于中途不能转向,把当前方向也塞到节点里。除1234分别对应上下左右外,新增一种......
  • 3.11每日总结
    净现值计算 #include<iostream>#include<iomanip>#include<cmath>constintPROJECTS=6;constintYEARS=4;intmain(){//创建二维数组储存每个项目每年利润intmoney[PROJECTS][YEARS]={{-100000,-1000000,-100000,-120000},{10000,......
  • 3.11
    今天实现通过安卓连接web后端最后在mysql数据库添加数据库的操作在安卓项目中首先在AndroidMainfest.xml中添加链接网络权限,同时允许安卓明文传输所要连接的IP地址<?xmlversion="1.0"encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/andro......
  • 洛谷题单指南-线性表-P1160 队列安排
    原题链接:https://www.luogu.com.cn/problem/P1160题意解读:本题是双向链表的模拟题,要快速实现M个节点的删除,用数组模拟链表是最佳做法。解题思路:双向链表关键要实现好两个操作:voidadd(intk,intv);//在第k个节点后增加第v的号节点,即在k号同学右边插入v号同学voiddel(int......
  • 软件工程日报5 2024.03.11
     第一天第二天第三天第四天第五天所花时间(包括上课)6小时5小时4小时4小时 六小时代码量(行)300350200300 50博客量(篇)1111 1所学知识了解安卓相关数据库的知识,下载安装了matlab学习了相关安卓的布局展示了解activity之间的相互跳转以注册了......
  • 11_redis事务
    redis事务数据库事务所有的数据库操作都必须一次性完成,要么成功,要么失败。redis事务可以一次执行多个命令,本质上是一组命令的集合。一个事务中的所有命令都会序列化,按顺序地串行化执行而不被其他命令插入,不许加载(不许被不属于该集合的命令插入)。开启:以MULTI开始一个事务入......
  • 日志
    LogBack配置文件Maven项目中xml要放在resource,文件夹下面<?xmlversion="1.0"encoding="UTF-8"?><configuration><!--CONSOLE:表示当前的日志信息是可以输出到控制台的。--><appendername="CONSOLE"class="ch.qos.logb......
  • 3.8~3.11闲话
    3.8因为教师资格证考试所以放假......
  • 鲜花 3.11
    这是一篇鲜花。我认为鲜花是相当好的,因为可以乐乐乐。最近精神状态一直不大好,连续n天没有写题了,鉴定为不打隔膜导致的/cf/cf/cf上周把WA2coda推完了,可能还有一个后日谈,这周回去推。怎么还有巨大的任务计划,破防了。看到hanghang的鲜花,感觉神秘敬酒环节有点可怕的,还好我......
  • 在 Windows 11 中卸载 Edge 浏览器
    请先安装一款其他的浏览器,例如:Chrome(非常重要);在任务栏上点击搜索图标,弹出的搜索框中输入"注册表编辑器"并打开;在注册表编辑器顶部的地址栏中分别输入:计算机\HKEY_CURRENT_USER\Software\Microsoft计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft计算机\HKEY_LOCAL_MACHINE\S......