android button 添加事件_Android数据库SQLite增改查删操作演示

SQLite增改查删操作演示

SQLite增改查删操作演示-leansmall

《1》 效果:

bcb559dc12b584999f4df182c8ae51e0.png

《2》 UI设计:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:layout_marginBottom="50dp"

>

android:id="@+id/textView"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center"

android:text="SQLite增改查删操作演示"

android:textColor="#03A9F4"

android:textSize="30sp" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="20dp"

android:orientation="horizontal">

android:id="@+id/inset_edittext"

android:layout_width="0dp"

android:layout_weight="5"

android:layout_height="wrap_content"

android:hint="请输入要插入的数据"

android:textColorHint="#CCCCCC"/>

android:id="@+id/insert"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="2"

android:text="增加" />

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:id="@+id/update_before_edittext"

android:layout_width="0dp"

android:layout_weight="2.5"

android:layout_height="wrap_content"

android:hint="请输入更新前的内容"

android:textColorHint="#CCCCCC"/>

android:id="@+id/update_after_edittext"

android:layout_width="0dp"

android:layout_weight="2.5"

android:layout_height="wrap_content"

android:hint="请输入更新后的内容"

android:textColorHint="#CCCCCC"/>

android:id="@+id/update"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="2"

android:text="修改" />

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:id="@+id/delete_edittext"

android:layout_width="0dp"

android:layout_weight="5"

android:layout_height="wrap_content"

android:hint="请输入要删除的内容"

android:textColorHint="#CCCCCC"/>

android:id="@+id/delete"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="2"

android:text="删除" />

android:id="@+id/query"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="查询所有"/>

android:id="@+id/clear"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="清除所有" />

android:id="@+id/textview"

android:layout_width="match_parent"

android:layout_height="300dp"

android:layout_marginVertical="50dp"

android:layout_marginLeft="20dp"

android:layout_marginRight="20dp"

android:hint="查询内容为空"

android:textColorHint="#CCCCCC"

android:textColor="#6666FF"

android:textSize="25dp"/>

《3》 代码设计:

//1. DatabaseHelper.java 设计

public class DatabaseHelper extends SQLiteOpenHelper{

public static final String TB_NAME = "user";

//带全部参数的构造函数,此构造函数必不可少

public DatabaseHelper(Context context, String name, CursorFactory factory, int version) {

super(context, name, factory, version);

}

@Override

public void onCreate(SQLiteDatabase db) {

//创建数据库的一个表格,表格名为user,执行sql语句

String sql = "CREATE TABLE IF NOT EXISTS " + TB_NAME +"name varchar(20)";

db.execSQL(sql);

}

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

db.execSQL("DROP TABLE IF EXISTS " + TB_NAME);

onCreate(db);

}

}

//2. MainActivity.java 设计

public class MainActivity extends AppCompatActivity{

//声明所有要用到的按钮,文本框,编辑框的对象

Button insert;

Button update;

Button delete;

Button query;

Button clear;

EditText insert_edittext;

EditText delete_edittext;

EditText update_before_edittext;

EditText update_after_edittext;

TextView textview;

DatabaseHelper dbHelper;

SQLiteDatabase db;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//根据Layout按钮id得到Java按钮对象

insert = (Button) findViewById(R.id.insert);//增加按钮

update = (Button) findViewById(R.id.update);//修改按钮

delete = (Button) findViewById(R.id.delete);//删除按钮

query = (Button) findViewById(R.id.query);//查询按钮

clear = (Button)findViewById(R.id.clear);//清除编辑框和文本框内容的按钮

//因为响应点击按钮事件时要操作文本输入框中的内容

// 所以要获取相应文本输入框的对象及其中输入内容

insert_edittext = (EditText)findViewById(R.id.inset_edittext);

delete_edittext = (EditText)findViewById(R.id.delete_edittext);

update_before_edittext = (EditText)findViewById(R.id.update_before_edittext);

update_after_edittext = (EditText)findViewById(R.id.update_after_edittext);

textview = (TextView)findViewById(R.id.textview);//放置查询内容的文本框

//依靠DatabaseHelper的构造函数创建数据库

dbHelper = new DatabaseHelper(MainActivity.this, "test_db

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Android 应用程序,演示如何使用 SQLite 数据库进行 CRUD 操作: 1. 创建一个新的 Android 项目,并在 build.gradle 文件中添加以下依赖: ```groovy dependencies { implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' implementation 'androidx.recyclerview:recyclerview:1.1.0' implementation 'androidx.cardview:cardview:1.0.0' implementation 'androidx.sqlite:sqlite:2.1.0' } ``` 2. 创建一个新的数据库帮助器类 DBHelper,继承自 SQLiteOpenHelper: ```java public class DBHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "my_db"; private static final int DATABASE_VERSION = 1; private static final String TABLE_NAME = "my_table"; private static final String COLUMN_ID = "_id"; private static final String COLUMN_NAME = "name"; private static final String COLUMN_AGE = "age"; private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_NAME + " TEXT, " + COLUMN_AGE + " INTEGER" + ")"; public DBHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_TABLE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db); } } ``` 3. 在 MainActivity 中,初始化 DBHelper 实例,并使用 SQLiteDatabase 实例进行 CRUD 操作: ```java public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private RecyclerView.Adapter adapter; private List<Person> personList = new ArrayList<>(); private DBHelper dbHelper; private SQLiteDatabase db; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dbHelper = new DBHelper(this); db = dbHelper.getWritableDatabase(); recyclerView = findViewById(R.id.recycler_view); adapter = new PersonAdapter(personList); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setAdapter(adapter); findViewById(R.id.button_add).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addPerson(); } }); findViewById(R.id.button_query).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { queryPerson(); } }); } private void addPerson() { ContentValues values = new ContentValues(); values.put(DBHelper.COLUMN_NAME, "Tom"); values.put(DBHelper.COLUMN_AGE, 20); db.insert(DBHelper.TABLE_NAME, null, values); } private void queryPerson() { Cursor cursor = db.query(DBHelper.TABLE_NAME, null, null, null, null, null, null); if (cursor.moveToFirst()) { personList.clear(); do { int id = cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_ID)); String name = cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_NAME)); int age = cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_AGE)); Person person = new Person(id, name, age); personList.add(person); } while (cursor.moveToNext()); adapter.notifyDataSetChanged(); } cursor.close(); } @Override protected void onDestroy() { super.onDestroy(); db.close(); dbHelper.close(); } } ``` 4. 创建一个 Person 类,用来封装数据库中的每一条记录: ```java public class Person { private int id; private String name; private int age; public Person(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public String getName() { return name; } public int getAge() { return age; } } ``` 5. 创建一个 RecyclerView.Adapter 类,用来展示数据库中的每一条记录: ```java public class PersonAdapter extends RecyclerView.Adapter<PersonAdapter.ViewHolder> { private List<Person> personList; public PersonAdapter(List<Person> personList) { this.personList = personList; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_person, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { Person person = personList.get(position); holder.textViewId.setText(String.valueOf(person.getId())); holder.textViewName.setText(person.getName()); holder.textViewAge.setText(String.valueOf(person.getAge())); } @Override public int getItemCount() { return personList.size(); } static class ViewHolder extends RecyclerView.ViewHolder { TextView textViewId; TextView textViewName; TextView textViewAge; public ViewHolder(@NonNull View itemView) { super(itemView); textViewId = itemView.findViewById(R.id.text_view_id); textViewName = itemView.findViewById(R.id.text_view_name); textViewAge = itemView.findViewById(R.id.text_view_age); } } } ``` 6. 创建一个 item_person.xml 布局文件,用来展示每一条记录: ```xml <?xml version="1.0" encoding="utf-8"?> <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="16dp"> <TextView android:id="@+id/text_view_id" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="ID"/> <TextView android:id="@+id/text_view_name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Name"/> <TextView android:id="@+id/text_view_age" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Age"/> </LinearLayout> </androidx.cardview.widget.CardView> ``` 7. 运行应用程序,点击“Add”按钮添加一条记录,点击“Query”按钮查询所有记录,并展示在 RecyclerView 中。 以上就是一个简单的 Android 应用程序,演示如何使用 SQLite 数据库进行 CRUD 操作
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值