Android 数据库:SQLite

数据库:SQLite

在学习Android 过程中,用到了SQLite。本文创建了简单的user表,实现了人员的增删改查。SQL部分肯定还有很多信息需要挖掘研究,有需要的朋友可以查询SQLite的官方文档和Android SQLite 的相关文档进行深入学习。

代码部分

MainActivity 部分

// MainActivity 
public class MainActivity extends AppCompatActivity {
  EditText code, name, age;
  EditText findCode;
  Button add, update, query, delete;
  TextView showBlock;
  SQLiteDatabase sqLiteDatabase;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // 初始化操作
    this.initView();
  }

  // View初始化
  private void initView() {
    MyDataBase myDataBase = new MyDataBase(this);

    code = findViewById(R.id.code);
    name = findViewById(R.id.name);
    age = findViewById(R.id.age);

    findCode = findViewById(R.id.findCode);

    add = findViewById(R.id.add);
    update = findViewById(R.id.update);
    query = findViewById(R.id.query);
    delete = findViewById(R.id.delete);
    showBlock = findViewById(R.id.showBlock);
    // 获取SQLiteDatabase,通过该对象就可以执行增删改查操作
    sqLiteDatabase = myDataBase.getWritableDatabase();
  }

  public void addTest(View view) {
    Toast.makeText(this, "test", Toast.LENGTH_SHORT).show();
  }

  // 新增
  public void addHandle(View view) {
    if (this.code.getText().toString().trim().equals("")) {
      Toast.makeText(this, "请输入code", Toast.LENGTH_SHORT).show();
      return;
    }
    int code = Integer.parseInt(this.code.getText().toString().trim());
    String name = this.name.getText().toString().trim();
    int age = Integer.parseInt(this.age.getText().toString().trim());
    String sql = "insert into user(code, name, age) values(?,?,?)";
    sqLiteDatabase.execSQL(
        sql,
        new Object[]{ code, name, age }
    );
    Toast.makeText(this, "新增成功", Toast.LENGTH_SHORT).show();
  }

  // 删除
  public void deleteHandle(View view) {
    if (this.findCode.getText().toString().trim().equals("")) {
      Toast.makeText(this, "请输入删除的code", Toast.LENGTH_SHORT).show();
      return;
    }

    int code = Integer.parseInt(this.findCode.getText().toString().trim());
    String sql = "delete from user where code=?";
    sqLiteDatabase.execSQL(sql, new Object[]{code});
    Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
  }

  // 修改
  public void updateHandle(View view) {
    if (this.findCode.getText().toString().trim().equals("")) {
      Toast.makeText(this, "请输入查询code", Toast.LENGTH_SHORT).show();
      return;
    }

    int code = Integer.parseInt(this.findCode.getText().toString().trim());
    String name = this.name.getText().toString().trim();
    int age = Integer.parseInt(this.age.getText().toString().trim());

    String sql = "update user set name=?, age=? where code=?";
    sqLiteDatabase.execSQL(sql, new Object[]{name, age, code});
    Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
    this.findHandle(view);
  }

  // 查找
  public void findHandle(View view) {
    showBlock.setText("");
    String code = this.findCode.getText().toString().trim();

    if (code.equals("")) {
      Toast.makeText(this, "请输入查询code", Toast.LENGTH_SHORT).show();
      return;
    }

    String sql = "select * from user where code=?";
    Cursor cursor = sqLiteDatabase.rawQuery(sql, new String[]{code});

    while (cursor.moveToNext()) {
      @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("name"));
      @SuppressLint("Range") int age = cursor.getInt(cursor.getColumnIndex("age"));
      showBlock.append("编号:" + code +"姓名:" + name + "\t" + "年龄: " + age + "\n");
    }

    if (showBlock.getText().toString().trim().equals("")) {
      showBlock.setText("暂无查询数据");
      return;
    }

    Toast.makeText(this, "查询成功", Toast.LENGTH_SHORT).show();
  }
}

数据库 MyDataBase部分

public class MyDataBase extends SQLiteOpenHelper {
  protected static String name = "john.db";
  protected static int version = 1;

  public MyDataBase(@Nullable Context context) {
    super(context, name, null, version);
  }

  @Override
  public void onCreate(SQLiteDatabase sqLiteDatabase) {
    String strSQL = "create table user(code Integer, name varchar(20), age Integer)";
    sqLiteDatabase.execSQL(strSQL);
  }

  @Override
  public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

  }
}

页面布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:padding="10dp"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:text="编号" />

        <EditText
            android:id="@+id/code"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="number"
            android:hint="输入编号"
            android:text="" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:text="姓名" />

        <EditText
            android:id="@+id/name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:hint="输入姓名"
            android:inputType="text"
            android:text="" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:text="年龄" />

        <EditText
            android:id="@+id/age"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:hint="输入年龄"
            android:inputType="number"
            android:text="" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:text="编号" />

        <EditText
            android:id="@+id/findCode"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:hint="输入查找编号"
            android:inputType="number"
            android:text="" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="addHandle"
            android:text="新增" />

        <Button
            android:id="@+id/delete"
            android:layout_marginStart="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="deleteHandle"
            android:text="删除" />

        <Button
            android:id="@+id/update"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginHorizontal="10dp"
            android:onClick="updateHandle"
            android:text="修改" />

        <Button
            android:id="@+id/query"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="findHandle"
            android:text="查询" />

    </LinearLayout>

    <TextView
        android:id="@+id/showBlock"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:hint="显示查询信息"
        android:text="" />
</LinearLayout>

实现效果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值