3.12

废话不多说 上代码

DBhelper 类

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","数据库版本已更新");
    }
    //数据库版本发生变化时调用
}

activity_xml

<?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>

mainactivity

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();
    }

 

这段代码是一个简单的Android应用程序,用于创建一个简单的字典应用。以下是对代码的简明清晰的解释:

1. 导入必要的Android库和类。
2. 创建了一个名为`DictActivity`的类,继承自`AppCompatActivity`。
3. 在`onCreate`方法中,设置了布局和初始化各种UI元素。
4. 创建了一个`DatabaseHelper`对象用于数据库操作。
5. 实现了四个按钮的点击事件,分别用于插入、更新、搜索和删除数据库中的数据。
6. 每个按钮点击事件中都会获取数据库实例,执行相应的操作,并最终调用`myShow`方法刷新显示数据。
7. `myShow`方法用于展示数据库中的所有数据。
8. 数据库操作包括插入数据、更新数据、根据姓名搜索数据和删除数据。
9. 在搜索数据时,根据输入的姓名进行搜索,显示对应的姓名和年龄。
10. 每次操作完成后都会关闭数据库连接,并清空输入框中的内容。

整体来说,这段代码实现了一个简单的字典应用,可以通过界面进行数据的插入、更新、搜索和删除操作,并实时展示数据库中的数据。

posted @ 2024-03-12 22:41  晨观夕  阅读(2)  评论(0编辑  收藏  举报