Sqlite实验

做一个教师管理系统。要求:

(1)使用Sqlite数据库,创建TeacherDB,包含teacher表,表中字段:教师号、姓名、性别、工资。

(2)包括增加、删除、修改、查询按钮。

(3)包括教师号、姓名、性别、工资4个输入框。

(4)当单击增加按钮时,将输入的教师号、姓名、性别、工资插入到teacher表中。

 (5)当单击删除按钮时,根据输入的教师号则从数据库中删除此条记录。

(6)当单击修改按钮时,根据输入的教师号、姓名、性别、工资修改到教师号对应的记录上,其中教师号不允许修改。

(7)当单击查询按钮时,根据教师号、姓名、性别模糊查询,并将结果显示到下面的文本区中。

项目结构:

配置文件:

<?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/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Test8"
        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>
        <activity android:name=".AddTeacher"/>
        <activity android:name=".DeleteTeacher"/>
        <activity android:name=".UpdateTeacher"/>
    </application>

</manifest>
AddTeacher.java:
package com.example.test8;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class AddTeacher extends AppCompatActivity {

    TeacherSQLiteOpenHelper teacherSQLiteOpenHelper;
    private SQLiteDatabase db;

    EditText addTeacherId;
    EditText addTeacherName;
    EditText addTeacherSex;
    EditText addTeacherWage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_teacher);
        teacherSQLiteOpenHelper = new TeacherSQLiteOpenHelper(this);
        db = teacherSQLiteOpenHelper.getWritableDatabase();
        addTeacherId = findViewById(R.id.addTeacherId);
        addTeacherName = findViewById(R.id.addName);
        addTeacherSex = findViewById(R.id.addTeacherSex);
        addTeacherWage = findViewById(R.id.addTeacherWage);
    }

    public void doAdd(View view){
        String id = addTeacherId.getText().toString();
        String name = addTeacherName.getText().toString();
        String sex = addTeacherSex.getText().toString();
        System.out.println(id+name+sex);
        int wage = Integer.parseInt(addTeacherWage.getText().toString());
        insertData(id,name,sex,wage);

        Intent i = new Intent();
        setResult(1,i);
        finish();
    }

    public void insertData(String teacherId, String name, String sex, int wage){
        db.execSQL("insert into teacher(teacherId, name, sex, wage) values(?, ?, ?, ?)",
                new Object[]{teacherId,name,sex,wage});
        Toast toast=Toast.makeText(getApplicationContext(), "添加成功!", Toast.LENGTH_SHORT);
        toast.show();
    }

}
DeleteTeacher.java:
package com.example.test8;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class DeleteTeacher extends AppCompatActivity {

    TeacherSQLiteOpenHelper teacherSQLiteOpenHelper;
    private SQLiteDatabase db;

    EditText deleteTeacherId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete_teacher);
        teacherSQLiteOpenHelper = new TeacherSQLiteOpenHelper(this);
        db = teacherSQLiteOpenHelper.getWritableDatabase();
        deleteTeacherId = findViewById(R.id.deleteTeacherId);
    }

    public void doDelete(View view){
        String id = deleteTeacherId.getText().toString();
        deleteData(id);

        Intent i = new Intent();
        setResult(2,i);
        finish();
    }

    public void deleteData(String teacherId){
        db.execSQL("delete from teacher where teacherId = ?", new Object[]{teacherId});
        Toast toast=Toast.makeText(getApplicationContext(), "删除成功!", Toast.LENGTH_SHORT);
        toast.show();
    }
}
UpdateTeacher.java:

package com.example.test8;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class UpdateTeacher extends AppCompatActivity {

    TeacherSQLiteOpenHelper teacherSQLiteOpenHelper;
    private SQLiteDatabase db;

    EditText updateTeacherId;
    EditText updateTeacherName;
    EditText updateTeacherSex;
    EditText updateTeacherWage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_teacher);
        teacherSQLiteOpenHelper = new TeacherSQLiteOpenHelper(this);
        db = teacherSQLiteOpenHelper.getWritableDatabase();
        updateTeacherId = findViewById(R.id.updateTeacherId);
        updateTeacherName = findViewById(R.id.updateName);
        updateTeacherSex = findViewById(R.id.updateTeacherSex);
        updateTeacherWage = findViewById(R.id.updateTeacherWage);
    }

    public void doUpdate(View view){
        String id = updateTeacherId.getText().toString();
        String name = updateTeacherName.getText().toString();
        String sex = updateTeacherSex.getText().toString();
        int wage = Integer.parseInt(updateTeacherWage.getText().toString());
        updateData(id,name,sex,wage);

        Intent i = new Intent();
        setResult(3,i);
        finish();
    }

    public void updateData(String teacherId, String name, String sex, int wage){
        db.execSQL("update teacher set name=?, sex=?, wage=? where teacherId=?",
                new Object[]{name,sex,wage,teacherId});
        Toast toast=Toast.makeText(getApplicationContext(), "更新成功!", Toast.LENGTH_SHORT);
        toast.show();
    }
}
TeacherSQLiteOpenHelper.java:
package com.example.test8;

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

public class TeacherSQLiteOpenHelper extends SQLiteOpenHelper {
    private Context context;
    //用来定义数据库的名称,数据库查询的结果集,数据库的版本
    public TeacherSQLiteOpenHelper(Context context){
        super(context,"TeacherDB.db",null,5);
        this.context = context;
    }
    //数据库第一次被创建时调用该方法
    public void onCreate(SQLiteDatabase db){
        //初始化数据库的表结构,执行一条建表的SQL语句
        db.execSQL("create table teacher" +
                "(id integer primary key autoincrement,teacherId varchar(20)," +
                "name varchar(20),sex varchar(20),wage int )");
    }
    //当数据库的版本号增加时调用
    public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
        db.execSQL("alter table teacher add account varchar(20)");
    }
}

activity_add_teacher.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".AddTeacher">

    <LinearLayout
        android:id="@+id/tvb"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="教师编号: "
                android:padding="20sp"/>
            <EditText
                android:id="@+id/addTeacherId"
                android:layout_width="257dp"
                android:layout_height="wrap_content"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="教师姓名: "
                android:padding="20sp"/>
            <EditText
                android:id="@+id/addName"
                android:layout_width="257dp"
                android:layout_height="wrap_content" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="教师性别: "
                android:padding="20sp"/>
            <EditText
                android:id="@+id/addTeacherSex"
                android:layout_width="257dp"
                android:layout_height="wrap_content" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="教师工资: "
                android:padding="20sp"/>
            <EditText
                android:id="@+id/addTeacherWage"
                android:layout_width="257dp"
                android:layout_height="wrap_content" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
            <Button
                android:layout_width="200sp"
                android:layout_height="wrap_content"
                android:text="提交添加"
                android:onClick="doAdd"/>
        </LinearLayout>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

activity_delete_teacher.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".DeleteTeacher">

    <LinearLayout
        android:id="@+id/tvb"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_marginTop="100sp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="教师编号: "
                android:padding="20sp"/>
            <EditText
                android:id="@+id/deleteTeacherId"
                android:layout_width="257dp"
                android:layout_height="wrap_content"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
            <Button
                android:layout_width="200sp"
                android:layout_height="wrap_content"
                android:text="提交删除"
                android:onClick="doDelete"
                android:layout_marginTop="20sp"/>
        </LinearLayout>
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

activity_update_teacher.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".UpdateTeacher">


    <LinearLayout
        android:id="@+id/tvb"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="教师编号: "
                android:padding="20sp"/>
            <EditText
                android:id="@+id/updateTeacherId"
                android:layout_width="257dp"
                android:layout_height="wrap_content"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="教师姓名: "
                android:padding="20sp"/>
            <EditText
                android:id="@+id/updateName"
                android:layout_width="257dp"
                android:layout_height="wrap_content" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="教师性别: "
                android:padding="20sp"/>
            <EditText
                android:id="@+id/updateTeacherSex"
                android:layout_width="257dp"
                android:layout_height="wrap_content" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="教师工资: "
                android:padding="20sp"/>
            <EditText
                android:id="@+id/updateTeacherWage"
                android:layout_width="257dp"
                android:layout_height="wrap_content" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
            <Button
                android:layout_width="200sp"
                android:layout_height="wrap_content"
                android:text="提交修改"
                android:onClick="doUpdate"/>
        </LinearLayout>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

message.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/ll_view"
        android:gravity="center"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:stretchColumns="*"
            android:padding="3dip"
            >
            <TableRow>
                <TextView
                    android:id="@+id/teacherId"
                    android:layout_width="1dip"
                    android:layout_height="match_parent"
                    android:textAlignment="center"
                    android:padding="10sp"
                    android:text="教师号"/>
                <TextView
                    android:id="@+id/name"
                    android:layout_width="1dip"
                    android:layout_height="match_parent"
                    android:textAlignment="center"
                    android:padding="10sp"
                    android:text="姓名"/>
                <TextView
                    android:id="@+id/sex"
                    android:layout_width="1dip"
                    android:layout_height="match_parent"
                    android:textAlignment="center"
                    android:padding="10sp"
                    android:text="性别"/>
                <TextView
                    android:id="@+id/wage"
                    android:layout_width="1dip"
                    android:layout_height="match_parent"
                    android:textAlignment="center"
                    android:padding="10sp"
                    android:text="工资"/>
            </TableRow>
        </TableLayout>
    </LinearLayout>
</LinearLayout>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/tva"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:stretchColumns="*"
            android:padding="3dip"
            >
            <TableRow>
                <TextView
                    android:layout_width="1dip"
                    android:layout_height="match_parent"
                    android:textAlignment="center"
                    android:padding="10sp"
                    android:text="教师号"
                    android:textColor="#000"
                    android:textSize="20sp"/>
                <TextView
                    android:layout_width="1dip"
                    android:layout_height="match_parent"
                    android:textAlignment="center"
                    android:padding="10sp"
                    android:text="姓名"
                    android:textColor="#000"
                    android:textSize="20sp"/>
                <TextView
                    android:layout_width="1dip"
                    android:layout_height="match_parent"
                    android:textAlignment="center"
                    android:padding="10sp"
                    android:text="性别"
                    android:textColor="#000"
                    android:textSize="20sp"/>
                <TextView
                    android:layout_width="1dip"
                    android:layout_height="match_parent"
                    android:textAlignment="center"
                    android:padding="10sp"
                    android:text="工资"
                    android:textColor="#000"
                    android:textSize="20sp"/>
            </TableRow>
        </TableLayout>

        <ListView
            android:id="@+id/mylistA"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        </ListView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <FrameLayout
                android:id="@+id/FrameLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_above="@+id/LinearLayout1"/>

            <TableLayout
                android:id="@+id/LinearLayout1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:stretchColumns="*"
                android:padding="3dip"
                android:layout_alignParentBottom="true"
                >
                <TableRow>
                    <Button
                        android:id="@+id/btnA"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="查询"
                        android:textColor="#fff"
                        android:background="#2C2E30"
                        android:paddingTop="10sp"
                        android:paddingBottom="10sp"/>
                    <Button

                        android:id="@+id/btnB"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="添加"
                        android:textColor="#fff"
                        android:background="#2C2E30"
                        android:paddingTop="10sp"
                        android:paddingBottom="10sp"/>
                    <Button
                        android:id="@+id/btnC"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="删除"
                        android:textColor="#fff"
                        android:background="#2C2E30"
                        android:paddingTop="10sp"
                        android:paddingBottom="10sp"/>
                    <Button
                        android:id="@+id/btnD"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="修改"
                        android:textColor="#fff"
                        android:background="#2C2E30"
                        android:paddingTop="10sp"
                        android:paddingBottom="10sp"/>
                </TableRow>
            </TableLayout>
        </RelativeLayout>

    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值