首页 > 其他分享 >软件工程日报——第十二天

软件工程日报——第十二天

时间:2023-03-07 20:57:32浏览次数:46  
标签:content layout 日报 setText cursor 软件工程 第十二天 android id

Android studio的数据库的增删改查功能实现

创建一个DatabaseHelper用来实现操作

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {

public DatabaseHelper(Context context){super(context,"Test.db",null,1);}
//第一个参数是上下文,第二个参数是数据库名称,
//第三个参数是CursorFactory对象,一般设置为null,第四个参数是数据库的版本
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE information(_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),age INTEGER)");
}
//创建表 表名information 表结构 自增id,字符串姓名,int年龄

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.d("myDeBug","数据库版本已更新");
}
//数据库版本发生变化时调用
}

DictActivity
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名"
android:textSize="30sp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名"
android:textSize="20sp"
android:id="@+id/name"
/>
</LinearLayout>
<LinearLayout

android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年龄"
android:textSize="30sp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入年龄"
android:textSize="20sp"
android:id="@+id/age"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:text="插入"
android:id="@+id/btn_insert"
android:textAllCaps="false"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:text="更新"
android:id="@+id/btn_update"
android:textAllCaps="false"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:text="查询"
android:id="@+id/btn_search"
android:textAllCaps="false"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:text="删除"
android:id="@+id/btn_delete"
android:textAllCaps="false"
/>


</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:layout_weight="1"
android:id="@+id/tv_show"
android:textSize="20sp"
android:gravity="center_horizontal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/tv_showAge"
android:textSize="20sp"
android:gravity="center_horizontal"
/>
</LinearLayout>


</LinearLayout>

    最外层垂直线性布局,内部三个线性布局,后两个用的水平排版。
    因为不清楚表格TextView怎么做,TableView太麻烦,索性两个TextView,weight都设置为1就可以了。

主要代码
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class DictActivity extends AppCompatActivity {
private Button insertButton,updateButton,searchButton,deleteButton;
private EditText name,age;
private TextView show,showAge;
final DatabaseHelper dbHelper=new DatabaseHelper(DictActivity.this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dict);

insertButton=findViewById(R.id.btn_insert);
updateButton=findViewById(R.id.btn_update);
searchButton=findViewById(R.id.btn_search);
deleteButton=findViewById(R.id.btn_delete);
name=findViewById(R.id.name);
age=findViewById(R.id.age);
show=findViewById(R.id.tv_show);
showAge=findViewById(R.id.tv_showAge);

 

SQLiteDatabase db=dbHelper.getReadableDatabase();

myShow();

insertButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

SQLiteDatabase db=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name",name.getText().toString());
values.put("age",age.getText().toString());
long id =db.insert("information",null,values);
Log.d("myDeBug","insert");

myShow();

db.close();
name.setText(null);
age.setText(null);
}
});
updateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

SQLiteDatabase db=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("age",age.getText().toString());
db.update("information",values,"name=?",new String[]{name.getText().toString()});

myShow();


db.close();
Log.d("myDebug","update");
name.setText(null);
age.setText(null);
}
});
searchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

SQLiteDatabase db=dbHelper.getWritableDatabase();
String name1=name.getText().toString();
show.setText(null);
if(name1.equals("")){
// show.setText("姓名");
// showAge.setText("年龄");
// Cursor cursor = db.rawQuery("select * from information",null);
//
// while (cursor.moveToNext()) {
// String newName = cursor.getString(cursor.getColumnIndex("name"));
// int newAge = cursor.getInt(cursor.getColumnIndex("age"));
// show.setText(show.getText() + "\n" + newName);
// showAge.setText(showAge.getText()+"\n" + newAge);
// }

myShow();

db.close();
}else {
show.setText("姓名");
showAge.setText("年龄");
Cursor cursor = db.rawQuery("select * from information where name = ? ", new String[]{name1});

while (cursor.moveToNext()) {
String newName = cursor.getString(cursor.getColumnIndex("name"));
int newAge = cursor.getInt(cursor.getColumnIndex("age"));
// show.setText(show.getText() + "\n" + newName + "\t" + newAge);
show.setText(show.getText() + "\n" + newName);
showAge.setText(showAge.getText()+"\n" + newAge);
}

cursor.close();
db.close();
name.setText(null);
age.setText(null);
}
}
});
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

SQLiteDatabase db=dbHelper.getWritableDatabase();
db.delete("information","name=?",new String[]{name.getText().toString()});


myShow();


db.close();
Log.d("myDeBug","DeleteSuccess");
name.setText(null);
age.setText(null);
}
});

}

public void myShow(){
SQLiteDatabase db=dbHelper.getReadableDatabase();


show.setText("姓名");
showAge.setText("年龄");
Cursor cursor = db.rawQuery("select * from information",null);

while (cursor.moveToNext()) {
String newName = cursor.getString(cursor.getColumnIndex("name"));
int newAge = cursor.getInt(cursor.getColumnIndex("age"));
show.setText(show.getText() + "\n" + newName);
showAge.setText(showAge.getText()+"\n" + newAge);
}
cursor.close();
}

    实例化四个Button,两个EditText,两个TextView,连接数据库
按钮增加监听点击事件,editText用来获取输入,TextView来展示成果。
    说一下myShow()方法,在这个里面我写了展示代码,先连接数据库,设置“表头”,用cursor来遍历表内所有信息,直到没有行,每过一行,获取name和age列的数据,设置TextView的文本(setText())之前的文本,换行,加上这行的数据。其他的增删改查,只是简单地执行sql语句或者调用SQLiteDatabase的方法。
在这里解释一下cursor
    Cursor默认是行的集合:
    查询出来的cursor的初始位置是指向第一条记录的前一个位置的
    cursor.moveToFirst()指向查询结果的第一个位置。
    一般通过判断cursor.moveToFirst()的值为true或false来确定查询结果是否为空。cursor.moveToNext()是用来做循环的,一般这样来用:while(cursor.moveToNext()){ }
    cursor.moveToPrevious()是指向当前记录的上一个记录,是和moveToNext相对应的;
    cursor.moveToLast()指向查询结果的最后一条记录
    使用cursor可以很方便的处理查询结果以便得到想要的数据
    上面关于cursor这部分来自Cursor.moveToNext();和Cursor.moveToFirst();

标签:content,layout,日报,setText,cursor,软件工程,第十二天,android,id
From: https://www.cnblogs.com/tianminggeng/p/17189612.html

相关文章

  • 软件工程日报十一——安卓studio的数据查询
    上一篇博客实现了安卓studio内数据的添加,这篇博客来实现数据的查询。mainactivity_main.xml文件<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android=......
  • 日报周报月报工作总结生成器「智能文案生成器」
    ​在职场上,尤其是互联网公司里,“写周报”是一件麻烦的事情。理想的工作环境下,写周报能让工作内容透明化,并有助于总结工作经验和办事方法。但现实情况往往没那么理想。......
  • 软件工程学习第十二天
    今天我们上课讲解了如何规范代码,规范代码十分重要。代码是需要维护的,无论是自己维护,还是其他人维护,都需要阅读代码。符合规范的代码,能减少理解成本。其次,代码规范最大的目......
  • 今日报告
    总结--课程挺多,注意力超级集中的一天!代码时间(包括上课):3h代码量(行):200行博客数量(篇):1篇了解到的相关知识点:1、人外有人,天外有天,总是有人会比我做得更好,看到同学们做出来......
  • 2023年3月6日软工日报
    今天上午没课,下午是建民的课,上课讲的是那个代码规范,后来就是验收app,app不怎么样,但是获得了加分  后来就是一道算法题  最厉害的解法就是:1publicclassSolut......
  • 2023年3月6日(软件工程日报)
    今天完成个人作业第一部分内容,其中最长打卡天数未能实现,打卡显示没有完成,在第二阶段会加以完善。以下为个人代码java代码方面,包括闹钟设定,登录设定,注册设定,封装类,连接数......
  • 2023.3.6软件工程日报
    所花时间:3小时 代码量:100行 博客量:1 今天由于课上验收加了0.5分日期为2023.3.6    此外看了其他优秀同学的作品,深感自己的差距,感觉应该更细化业务逻辑......
  • 今日报告-14
    今日打卡所花时间(包括上课):6h代码量(行):400发表博客:3篇(不包括本篇)了解到的知识点:继续Android学习,接着写每日打卡app,同时仔细学习了Fragment,ListView相关知识,打了许多练......
  • 今日报告
    关键字总结--成就感加满一半儿的一天代码时间(包括上课):5h代码量(行):100行博客数量(篇):2篇了解到的相关知识点:1、Androidstudio跳转界面如何传值(就像javaweb里面的session......
  • 对现代软件工程基础的部分了解
    1、软件工程的目标有哪些?就当今社会而言,软件工程仍处于一个上升阶段,为获得更快速的发展,我们需要为它定制目标。软件工程要达到的基本目标包括:达到要求的软件功能、取得较好......