ToB企服应用市场:ToB评测及商务社交产业平台

标题: 基于AndroidStudio内置数据库SQLite,构建简单实现数据库增编削查功能APP [打印本页]

作者: 来自云龙湖轮廓分明的月亮    时间: 2024-12-11 01:30
标题: 基于AndroidStudio内置数据库SQLite,构建简单实现数据库增编削查功能APP
Android提供了SQLiteDatabase代表一个数据库(底层就是一个数据库文件),一旦应用程序获得了代表指定命据库的SQLiteDatabase对象,接下来可通过SQLiteDatabase对象来管理、操作数据库。
Android中使用SQLite数据库进行开发时,重要利用SQL语句来进行基本功能实现。掌握如下增编削查语句更利于学习。
创建表create table tableName(id integer primary key autoincrement , name , text , number text);
删除表 drop table if exists tableName;
增长一条数据:insert into tableName values(“小明”,”111”);
删除一条数据:delete from tableName where name=”小明”;
修改一条数据:update tableName set name=”小红” where name=”小明”;
查询一条数据:select*from tableName where name=”小明”;
首页

结果图:

MainActivity.java

  1. package com.example.sqlite_foundation;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. public class MainActivity extends AppCompatActivity {
  7.     @Override
  8.     protected void onCreate(Bundle savedInstanceState) {
  9.         super.onCreate(savedInstanceState);
  10.         setContentView(R.layout.activity_main);
  11.     }
  12.     public void insertData(View view){
  13.         //进行跳转
  14.         Intent intent = new Intent(this,insertActivity.class);
  15.         startActivity(intent);
  16.     }
  17.     public void deleteData(View view){
  18.         Intent intent = new Intent(this,DeleteActivity.class);
  19.         startActivity(intent);
  20.     }
  21.     public void updateData(View view){
  22.         Intent intent = new Intent(this,UpdateActivity.class);
  23.         startActivity(intent);
  24.     }
  25.     public void queryData(View view){
  26.         Intent intent = new Intent(this,QueryActivity.class);
  27.         startActivity(intent);
  28.     }
  29. }
复制代码
activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical"
  6.     android:gravity="center"
  7.     android:background="@drawable/back"
  8.     android:scaleType="matrix"
  9.     >
  10.    <Button
  11.        android:layout_width="300dp"
  12.        android:layout_height="60dp"
  13.        android:onClick="insertData"
  14.        android:text="添加数据"
  15.        android:textSize="20sp"
  16.        android:textStyle="bold"
  17.        android:layout_gravity="center"
  18.        android:background="@color/yellow"/>
  19.     <Button
  20.         android:layout_width="300dp"
  21.         android:layout_height="60dp"
  22.         android:onClick="deleteData"
  23.         android:text="删除数据"
  24.         android:textSize="20sp"
  25.         android:textStyle="bold"
  26.         android:layout_gravity="center"
  27.         android:layout_marginTop="20dp"
  28.         android:background="@color/yellow"/>
  29.     <Button
  30.         android:layout_width="300dp"
  31.         android:layout_height="60dp"
  32.         android:onClick="updateData"
  33.         android:text="修改数据"
  34.         android:textSize="20sp"
  35.         android:textStyle="bold"
  36.         android:layout_gravity="center"
  37.         android:layout_marginTop="20dp"
  38.         android:background="@color/yellow"/>
  39.     <Button
  40.         android:layout_width="300dp"
  41.         android:layout_height="60dp"
  42.         android:onClick="queryData"
  43.         android:text="查询数据"
  44.         android:textSize="20sp"
  45.         android:textStyle="bold"
  46.         android:layout_gravity="center"
  47.         android:layout_marginTop="20dp"
  48.         android:background="@color/yellow"/>
  49. </LinearLayout>
复制代码
数据库创建

MySQLiteOpenHerper.java

  1. package com.example.sqlite_foundation;
  2. import android.content.ContentValues;
  3. import android.content.Context;
  4. import android.database.Cursor;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.database.sqlite.SQLiteOpenHelper;
  7. import androidx.annotation.Nullable;
  8. import com.example.sqlite_foundation.bean.Student;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. public class MySQLiteOpenHelper extends SQLiteOpenHelper {
  12.     private static final String DB_NAME="mySQLite.db";
  13.     private static final String TABLE_NAME_STUDENT="student";
  14.     //SQL语句准备
  15.     private static final String CREATE_TABLE_SQL = "create table " + TABLE_NAME_STUDENT + " (id integer primary key autoincrement,name text,number text,gender text,score text);";
  16.     //定死参数
  17.     public MySQLiteOpenHelper(Context context){
  18.         super(context,DB_NAME,null,1);
  19.     }
  20.     @Override
  21.     public void onCreate(SQLiteDatabase sqLiteDatabase) {
  22.         //执行SQL语句
  23.         sqLiteDatabase.execSQL(CREATE_TABLE_SQL);
  24.     }
  25.     @Override
  26.     public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
  27.     }
  28.     //插入数据方法
  29.     public long insertData(Student student){
  30.         //得到一个可写数据库
  31.         SQLiteDatabase db = getWritableDatabase();
  32.         ContentValues values = new ContentValues();
  33.         //传值准备数据
  34.         values.put("name",student.getName());
  35.         values.put("number",student.getNumber());
  36.         values.put("gender",student.getGender());
  37.         values.put("score",student.getScore());
  38.         //把数据插入到数据库中
  39.         //返回当前行的id,若为-1则数据插入失败
  40.         return db.insert(TABLE_NAME_STUDENT,null,values);
  41.     }
  42.     //按姓名删除数据方法
  43.     public int deleteFromDbByName(String name){
  44.         SQLiteDatabase db = getWritableDatabase();
  45.         return db.delete(TABLE_NAME_STUDENT,"name like ?",new String[]{name});
  46.     }
  47.     //修改数据方法
  48.     public int updateData(Student student){
  49.         SQLiteDatabase db = getWritableDatabase();
  50.         ContentValues values = new ContentValues();
  51.         //传值准备数据
  52.         values.put("name",student.getName());
  53.         values.put("number",student.getNumber());
  54.         values.put("gender",student.getGender());
  55.         values.put("score",student.getScore());
  56.         return db.update(TABLE_NAME_STUDENT,values,"name like ?",new String[] {student.getName()});
  57.     }
  58.     //查询数据方法
  59.     public List<Student> queryFromDbByName(String name){
  60.         SQLiteDatabase db = getWritableDatabase();
  61.         List<Student> studentList = new ArrayList<>();
  62.         Cursor cursor = db.query(TABLE_NAME_STUDENT, null, "name like ?", new String[]{name}, null, null, null);
  63.         if(cursor!=null){
  64.             while(cursor.moveToNext()){
  65.                 String name1 = cursor.getString(cursor.getColumnIndex("name"));
  66.                 String number = cursor.getString(cursor.getColumnIndex("number"));
  67.                 String gender = cursor.getString(cursor.getColumnIndex("gender"));
  68.                 String score = cursor.getString(cursor.getColumnIndex("score"));
  69.                 Student student = new Student();
  70.                 student.setName(name1);
  71.                 student.setNumber(number);
  72.                 student.setGender(gender);
  73.                 student.setScore(score);
  74.                 studentList.add(student);
  75.             }
  76.             cursor.close();
  77.         }
  78.         return studentList;
  79.     }
  80.     public List<Student> queryAllFromDb(){
  81.         SQLiteDatabase db = getWritableDatabase();
  82.         List<Student> studentList = new ArrayList<>();
  83.         Cursor cursor = db.query(TABLE_NAME_STUDENT, null, null,null, null, null, null);
  84.         if(cursor!=null){
  85.             while(cursor.moveToNext()){
  86.                 String name1 = cursor.getString(cursor.getColumnIndex("name"));
  87.                 String number = cursor.getString(cursor.getColumnIndex("number"));
  88.                 String gender = cursor.getString(cursor.getColumnIndex("gender"));
  89.                 String score = cursor.getString(cursor.getColumnIndex("score"));
  90.                 Student student = new Student();
  91.                 student.setName(name1);
  92.                 student.setNumber(number);
  93.                 student.setGender(gender);
  94.                 student.setScore(score);
  95.                 studentList.add(student);
  96.             }
  97.             cursor.close();
  98.         }
  99.         return studentList;
  100.     }
  101. }
复制代码
添加数据

结果图:

insertActivity.java

  1. package com.example.sqlite_foundation;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.EditText;
  6. import android.widget.RadioButton;
  7. import android.widget.Toast;
  8. import com.example.sqlite_foundation.bean.Student;
  9. import com.example.sqlite_foundation.util.ToastUtil;
  10. public class insertActivity extends AppCompatActivity {
  11.     private EditText etName,etNumber,etScore;
  12.     private RadioButton rbMan,rbWoman;
  13.     private MySQLiteOpenHelper mySQLiteOpenHelper;
  14.     @Override
  15.     protected void onCreate(Bundle savedInstanceState) {
  16.         super.onCreate(savedInstanceState);
  17.         setContentView(R.layout.activity_insert);
  18.         initView();
  19.         mySQLiteOpenHelper = new MySQLiteOpenHelper(this);
  20.     }
  21.     private void initView() {
  22.         etName = (EditText) findViewById(R.id.et_name);
  23.         etNumber = (EditText) findViewById(R.id.et_number);
  24.         etScore = (EditText) findViewById(R.id.et_score);
  25.         rbMan = (RadioButton) findViewById(R.id.rb_man);
  26.         rbWoman = (RadioButton) findViewById(R.id.rb_woman);
  27.     }
  28.     public void insert(View view){
  29.         String name = etName.getText().toString().trim();
  30.         String number = etNumber.getText().toString().trim();
  31.         String score = etScore.getText().toString().trim();
  32.         String gender="";
  33.         if(rbMan.isChecked()){
  34.             gender="男";
  35.         }
  36.         if(rbWoman.isChecked()){
  37.             gender="女";
  38.         }
  39.         //构建数据实体Student
  40.         Student student = new Student();
  41.         student.setName(name);
  42.         student.setNumber(number);
  43.         student.setGender(gender);
  44.         student.setScore(score);
  45.         //数据插入数据库中
  46.         long rowId = mySQLiteOpenHelper.insertData(student);
  47.         if(rowId!=-1){
  48.             //工具类ToastUtil的使用
  49.             ToastUtil.toastShort(this,"添加成功!");
  50.         }else{
  51.             ToastUtil.toastShort(this,"添加失败!");
  52.         }
  53.     }
  54. }
复制代码
activity_insert.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     android:orientation="vertical"
  8.     tools:context=".insertActivity">
  9.     <LinearLayout
  10.         android:layout_width="match_parent"
  11.         android:layout_height="wrap_content"
  12.         android:orientation="horizontal"
  13.         android:padding="10dp">
  14.         <TextView
  15.             android:layout_width="wrap_content"
  16.             android:layout_height="wrap_content"
  17.             android:maxLines="1"
  18.             style="@style/MyTextStyle"
  19.             android:text="姓名"/>
  20.         <EditText
  21.             android:id="@+id/et_name"
  22.             android:layout_width="match_parent"
  23.             android:layout_height="50dp"
  24.             android:textSize="18sp"
  25.             android:layout_marginLeft="10dp"
  26.             android:background="@drawable/edittext_border"/>
  27.     </LinearLayout>
  28.     <LinearLayout
  29.         android:layout_width="match_parent"
  30.         android:layout_height="wrap_content"
  31.         android:orientation="horizontal"
  32.         android:padding="10dp">
  33.         <TextView
  34.             android:layout_width="wrap_content"
  35.             android:layout_height="wrap_content"
  36.             android:maxLines="1"
  37.             android:inputType="number"
  38.             style="@style/MyTextStyle"
  39.             android:text="学号"/>
  40.         <EditText
  41.             android:id="@+id/et_number"
  42.             android:layout_width="match_parent"
  43.             android:layout_height="50dp"
  44.             android:textSize="18sp"
  45.             android:layout_marginLeft="10dp"
  46.             android:background="@drawable/edittext_border"/>
  47.     </LinearLayout>
  48.     <LinearLayout
  49.         android:layout_width="match_parent"
  50.         android:layout_height="wrap_content"
  51.         android:orientation="horizontal"
  52.         android:padding="10dp">
  53.         <TextView
  54.             android:layout_width="wrap_content"
  55.             android:layout_height="wrap_content"
  56.             android:maxLines="1"
  57.             style="@style/MyTextStyle"
  58.             android:text="性别"/>
  59.         <RadioGroup
  60.             android:layout_width="match_parent"
  61.             android:layout_height="wrap_content"
  62.             android:orientation="horizontal"
  63.             android:gravity="center"
  64.             android:layout_marginTop="10dp">
  65.             <RadioButton
  66.                 android:id="@+id/rb_man"
  67.                 android:layout_width="wrap_content"
  68.                 android:layout_height="wrap_content"
  69.                 android:text="男"/>
  70.             <RadioButton
  71.                 android:id="@+id/rb_woman"
  72.                 android:layout_width="wrap_content"
  73.                 android:layout_height="wrap_content"
  74.                 android:text="女"
  75.                 android:layout_marginLeft="30dp"/>
  76.         </RadioGroup>
  77.     </LinearLayout>
  78.     <LinearLayout
  79.         android:layout_width="match_parent"
  80.         android:layout_height="wrap_content"
  81.         android:orientation="horizontal"
  82.         android:padding="10dp">
  83.         <TextView
  84.             android:layout_width="wrap_content"
  85.             android:layout_height="wrap_content"
  86.             android:maxLines="1"
  87.             style="@style/MyTextStyle"
  88.             android:text="分数"/>
  89.         <EditText
  90.             android:id="@+id/et_score"
  91.             android:layout_width="match_parent"
  92.             android:layout_height="50dp"
  93.             android:textSize="18sp"
  94.             android:layout_marginLeft="10dp"
  95.             android:background="@drawable/edittext_border"/>
  96.     </LinearLayout>
  97.     <Button
  98.         android:text="保存"
  99.         android:onClick="insert"
  100.         style="@style/MyButton"
  101.         android:background="@drawable/round_button"
  102.         android:layout_marginTop="30dp"
  103.         />
  104. </LinearLayout>
复制代码
删除数据

结果图:

DeleteActivity.java

  1. package com.example.sqlite_foundation;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.EditText;
  6. import com.example.sqlite_foundation.util.ToastUtil;
  7. public class DeleteActivity extends AppCompatActivity {
  8.     private EditText etName;
  9.     private MySQLiteOpenHelper mySQLiteOpenHelper;
  10.     @Override
  11.     protected void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.activity_delete);
  14.         etName = findViewById(R.id.et_name);
  15.         mySQLiteOpenHelper = new MySQLiteOpenHelper(this);
  16.     }
  17.     public void delete(View view){
  18.         String name = etName.getText().toString().trim();
  19.         //按姓名从数据库中删除数据
  20.         int row = mySQLiteOpenHelper.deleteFromDbByName(name);
  21.         if(row>0){
  22.             ToastUtil.toastShort(this,"删除成功,删除了"+ row + "条数据!");
  23.         }else{
  24.             ToastUtil.toastShort(this,"删除失败,没有符合条件的数据!");
  25.         }
  26.     }
  27. }
复制代码
activity_delete.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     android:orientation="vertical"
  8.     tools:context=".DeleteActivity">
  9.     <LinearLayout
  10.         android:layout_width="match_parent"
  11.         android:layout_height="wrap_content"
  12.         android:orientation="horizontal"
  13.         android:padding="10dp">
  14.         <TextView
  15.             android:layout_width="wrap_content"
  16.             android:layout_height="wrap_content"
  17.             android:maxLines="1"
  18.             style="@style/MyTextStyle"
  19.             android:text="姓名"/>
  20.         <EditText
  21.             android:id="@+id/et_name"
  22.             android:layout_width="match_parent"
  23.             android:layout_height="50dp"
  24.             android:textSize="18sp"
  25.             android:layout_marginLeft="10dp"
  26.             android:background="@drawable/edittext_border"/>
  27.     </LinearLayout>
  28.     <Button
  29.         android:text="删除"
  30.         android:onClick="delete"
  31.         style="@style/MyButton"
  32.         android:background="@drawable/round_button"
  33.         android:layout_marginTop="30dp"
  34.         />
  35. </LinearLayout>
复制代码
修改数据

结果图:

UpdateActivity.java

  1. package com.example.sqlite_foundation;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.EditText;
  6. import android.widget.RadioButton;
  7. import com.example.sqlite_foundation.bean.Student;
  8. import com.example.sqlite_foundation.util.ToastUtil;
  9. public class UpdateActivity extends AppCompatActivity {
  10.     private EditText etName,etNumber,etScore;
  11.     private RadioButton rbMan,rbWoman;
  12.     private MySQLiteOpenHelper mySQLiteOpenHelper;
  13.     @Override
  14.     protected void onCreate(Bundle savedInstanceState) {
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.activity_update);
  17.         initView();
  18.         mySQLiteOpenHelper = new MySQLiteOpenHelper(this);
  19.     }
  20.     private void initView() {
  21.         etName = (EditText) findViewById(R.id.et_name);
  22.         etNumber = (EditText) findViewById(R.id.et_number);
  23.         etScore = (EditText) findViewById(R.id.et_score);
  24.         rbMan = (RadioButton) findViewById(R.id.rb_man);
  25.         rbWoman = (RadioButton) findViewById(R.id.rb_woman);
  26.     }
  27.     public void update(View view){
  28.         String name = etName.getText().toString().trim();
  29.         String number = etNumber.getText().toString().trim();
  30.         String score = etScore.getText().toString().trim();
  31.         String gender="";
  32.         if(rbMan.isChecked()){
  33.             gender="男";
  34.         }
  35.         if(rbWoman.isChecked()){
  36.             gender="女";
  37.         }
  38.         //构建数据实体Student
  39.         Student student = new Student();
  40.         student.setName(name);
  41.         student.setNumber(number);
  42.         student.setGender(gender);
  43.         student.setScore(score);
  44.         //更新数据库数据
  45.         long rowId = mySQLiteOpenHelper.updateData(student);
  46.         if(rowId>0){
  47.             //工具类ToastUtil的使用
  48.             ToastUtil.toastShort(this,"更新成功!");
  49.         }else{
  50.             ToastUtil.toastShort(this,"没有数据被更新!");
  51.         }
  52.     }
  53. }
复制代码
activity_update.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     android:orientation="vertical"
  8.     tools:context=".UpdateActivity">
  9.     <LinearLayout
  10.         android:layout_width="match_parent"
  11.         android:layout_height="wrap_content"
  12.         android:orientation="horizontal"
  13.         android:padding="10dp">
  14.         <TextView
  15.             android:layout_width="wrap_content"
  16.             android:layout_height="wrap_content"
  17.             android:maxLines="1"
  18.             style="@style/MyTextStyle"
  19.             android:text="姓名"/>
  20.         <EditText
  21.             android:id="@+id/et_name"
  22.             android:layout_width="match_parent"
  23.             android:layout_height="50dp"
  24.             android:textSize="18sp"
  25.             android:layout_marginLeft="10dp"
  26.             android:background="@drawable/edittext_border"/>
  27.     </LinearLayout>
  28.     <LinearLayout
  29.         android:layout_width="match_parent"
  30.         android:layout_height="wrap_content"
  31.         android:orientation="horizontal"
  32.         android:padding="10dp">
  33.         <TextView
  34.             android:layout_width="wrap_content"
  35.             android:layout_height="wrap_content"
  36.             android:maxLines="1"
  37.             android:inputType="number"
  38.             style="@style/MyTextStyle"
  39.             android:text="学号"/>
  40.         <EditText
  41.             android:id="@+id/et_number"
  42.             android:layout_width="match_parent"
  43.             android:layout_height="50dp"
  44.             android:textSize="18sp"
  45.             android:layout_marginLeft="10dp"
  46.             android:background="@drawable/edittext_border"/>
  47.     </LinearLayout>
  48.     <LinearLayout
  49.         android:layout_width="match_parent"
  50.         android:layout_height="wrap_content"
  51.         android:orientation="horizontal"
  52.         android:padding="10dp">
  53.         <TextView
  54.             android:layout_width="wrap_content"
  55.             android:layout_height="wrap_content"
  56.             android:maxLines="1"
  57.             style="@style/MyTextStyle"
  58.             android:text="性别"/>
  59.         <RadioGroup
  60.             android:layout_width="match_parent"
  61.             android:layout_height="wrap_content"
  62.             android:orientation="horizontal"
  63.             android:gravity="center"
  64.             android:layout_marginTop="10dp">
  65.             <RadioButton
  66.                 android:id="@+id/rb_man"
  67.                 android:layout_width="wrap_content"
  68.                 android:layout_height="wrap_content"
  69.                 android:text="男"/>
  70.             <RadioButton
  71.                 android:id="@+id/rb_woman"
  72.                 android:layout_width="wrap_content"
  73.                 android:layout_height="wrap_content"
  74.                 android:text="女"
  75.                 android:layout_marginLeft="30dp"/>
  76.         </RadioGroup>
  77.     </LinearLayout>
  78.     <LinearLayout
  79.         android:layout_width="match_parent"
  80.         android:layout_height="wrap_content"
  81.         android:orientation="horizontal"
  82.         android:padding="10dp">
  83.         <TextView
  84.             android:layout_width="wrap_content"
  85.             android:layout_height="wrap_content"
  86.             android:maxLines="1"
  87.             style="@style/MyTextStyle"
  88.             android:text="分数"/>
  89.         <EditText
  90.             android:id="@+id/et_score"
  91.             android:layout_width="match_parent"
  92.             android:layout_height="50dp"
  93.             android:textSize="18sp"
  94.             android:layout_marginLeft="10dp"
  95.             android:background="@drawable/edittext_border"/>
  96.     </LinearLayout>
  97.     <Button
  98.         android:text="更新"
  99.         android:onClick="update"
  100.         style="@style/MyButton"
  101.         android:background="@drawable/round_button"
  102.         android:layout_marginTop="30dp"
  103.         />
  104. </LinearLayout>
复制代码
查询数据

结果图:

QueryActivity.java

  1. package com.example.sqlite_foundation;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.text.TextUtils;
  5. import android.view.View;
  6. import android.widget.EditText;
  7. import android.widget.TextView;
  8. import com.example.sqlite_foundation.bean.Student;
  9. import com.example.sqlite_foundation.util.ToastUtil;
  10. import java.util.List;
  11. public class QueryActivity extends AppCompatActivity {
  12.     private EditText etName;
  13.     private TextView tvResult;
  14.     private MySQLiteOpenHelper mySQLiteOpenHelper;
  15.     @Override
  16.     protected void onCreate(Bundle savedInstanceState) {
  17.         super.onCreate(savedInstanceState);
  18.         setContentView(R.layout.activity_query);
  19.         etName = findViewById(R.id.et_name);
  20.         tvResult = findViewById(R.id.tv_result);
  21.         mySQLiteOpenHelper = new MySQLiteOpenHelper(this);
  22.     }
  23.     public void query(View view) {
  24.         String name = etName.getText().toString().trim();
  25.         if(TextUtils.isEmpty(name)){
  26.             //如果输入为空,则查询所有数据
  27.             List<Student> students = mySQLiteOpenHelper.queryAllFromDb();
  28.             showData(students);
  29.             return;
  30.         }
  31.         //按姓名在数据库中查询数据并显示
  32.         List<Student> students = mySQLiteOpenHelper.queryFromDbByName(name);
  33.         showData(students);
  34.     }
  35.     //展示数据方法
  36.     public void showData(List<Student> students){
  37. //        String result="";
  38. //        for(Student stu : students){
  39. //            result += "姓名:" +stu.getName()+",学号:"+stu.getNumber()+",性别:"+stu.getGender()+",分数:"+stu.getScore()+"\n";
  40. //        }
  41.         //因为每次String拼接都会new一个新的串,比较消耗性能,可以采用StringBuilder
  42.         StringBuilder stringBuilder = new StringBuilder();
  43.         for(Student stu : students){
  44.             stringBuilder.append("姓名:");
  45.             stringBuilder.append(stu.getName());
  46.             stringBuilder.append(",学号:");
  47.             stringBuilder.append(stu.getNumber());
  48.             stringBuilder.append(",性别:");
  49.             stringBuilder.append(stu.getGender());
  50.             stringBuilder.append(",分数:");
  51.             stringBuilder.append(stu.getScore());
  52.             stringBuilder.append("\n");
  53.         }
  54.         tvResult.setText(stringBuilder.toString());
  55.     }
  56. }
复制代码
activity_query.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     android:orientation="vertical"
  8.     tools:context=".QueryActivity"
  9.     >
  10.     <LinearLayout
  11.         android:layout_width="match_parent"
  12.         android:layout_height="wrap_content"
  13.         android:orientation="horizontal"
  14.         android:padding="10dp">
  15.         <TextView
  16.             android:layout_width="wrap_content"
  17.             android:layout_height="wrap_content"
  18.             android:maxLines="1"
  19.             style="@style/MyTextStyle"
  20.             android:text="姓名"/>
  21.         <EditText
  22.             android:id="@+id/et_name"
  23.             android:layout_width="match_parent"
  24.             android:layout_height="50dp"
  25.             android:textSize="18sp"
  26.             android:layout_marginLeft="10dp"
  27.             android:paddingLeft="5px"
  28.             android:background="@drawable/edittext_border"/>
  29.     </LinearLayout>
  30.     <Button
  31.         android:text="查询"
  32.         android:onClick="query"
  33.         style="@style/MyButton"
  34.         android:background="@drawable/round_button"
  35.         android:layout_marginTop="30dp"
  36.         />
  37.     <TextView
  38.         android:id="@+id/tv_result"
  39.         android:layout_width="wrap_content"
  40.         android:layout_height="wrap_content"
  41.         android:textColor="@color/black"
  42.         android:textSize="20sp"
  43.         android:layout_marginTop="20dp"
  44.         android:layout_marginLeft="10dp"
  45.         android:gravity="left"/>
  46. </LinearLayout>
复制代码
写在后面

1、频繁使用的Toast可以写一个工具类来简便代码。
  1. package com.example.sqlite_foundation.util;
  2. import android.content.Context;
  3. import android.widget.Toast;
  4. public class ToastUtil {
  5.     public static void toastShort(Context context,String msg){
  6.         Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
  7.     }
  8.     public static void toastLong(Context context,String msg){
  9.         Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
  10.     }
  11. }
复制代码
2、查询全部数据时的简单拼接方法由于每次String拼接都会new一个新串,较斲丧性能,可以接纳StringBuilder进行替换。

3、检察当前db数据库时可以采用点击菜单栏view->ToolWindows->database inspector进行检察。有些版本过高找不到database inspector,可以找到app inspection打开后运行app,检察。

[code][/code]
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4