软件工程日报十一——安卓studio的数据查询

上一篇博客实现了安卓studio内数据的添加,这篇博客来实现数据的查询。

mainactivity_main.xml文件

<?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:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">
<ImageView
    android:layout_gravity="center"
    android:background="@mipmap/ic_launcher"
    android:layout_width="100dp"
    android:layout_height="100dp">

</ImageView>
    <EditText
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名">

    </EditText>
    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="请输入密码">

    </EditText>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="条件查询"
        android:id="@+id/generate">

    </Button>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入用户名:"
        android:id="@+id/selection">

    </EditText>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/add"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="添加">

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

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

        </Button>
        <Button
            android:id="@+id/select"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="查询">

        </Button>

    </LinearLayout>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示查询结果!"
        android:textSize="25sp"
        android:id="@+id/showInfo">


    </TextView>


</LinearLayout>

运行界面如下:

 

 

Mainactivity源文件如下:


package com.example.sqlitetest;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button generateBtn,addBtn,deleteBtn,updateBtn,selectBtn;
private EditText et_username,et_password,et_selection;
private TextView showInfo;
private MyDbHelper myDbhelper;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDbhelper=new MyDbHelper(MainActivity.this,"MyDatabase.db",null,666);
initView();
generateBtn.setOnClickListener(this);
addBtn.setOnClickListener(this);
selectBtn.setOnClickListener(this);
}

@SuppressLint("WrongViewCast")
private void initView() {
generateBtn=findViewById(R.id.generate);
addBtn=findViewById(R.id.add);
deleteBtn=findViewById(R.id.delete);
updateBtn=findViewById(R.id.update);
selectBtn=findViewById(R.id.select);
et_username=findViewById(R.id.username);
et_password=findViewById(R.id.password);
showInfo=findViewById(R.id.showInfo);
et_selection=findViewById(R.id.selection);
}

@Override
public void onClick(View v) {
switch(v.getId())
{

case R.id.add:
//通过帮助类获取数据对象
db=myDbhelper.getWritableDatabase();
String username=et_username.getText().toString();
String password=et_password.getText().toString();

/* //创建一个ContentValues对象,用于存储记录的字段值,以键值对的方式储存,“键”对应就是字段名,“值”对应就是某个字段具体的值
ContentValues contentValues=new ContentValues();

contentValues.put("userName",username);
contentValues.put("password",password);
db.insert("user",null,contentValues);*/
db.execSQL("insert into user(userName,password) values(?,?)",new Object[]{username,password});
db.close();
break;
case R.id.select://普通查询
db=myDbhelper.getWritableDatabase();
Cursor cursor=db.query("user",new String[]{"username","password"},null,null,null,null,null,null);
cursor.moveToFirst();
showInfo.setText("用户名"+cursor.getString(0)+",密码"+cursor.getString(1));
while(cursor.moveToNext())
{
showInfo.append("\n"+"用户名"+cursor.getString(0)+",密码"+cursor.getString(1));
}
cursor.close();
db.close();
break;
case R.id.generate://条件查询
db=myDbhelper.getWritableDatabase();
String selection=et_selection.getText().toString();
//Cursor cursor1=db.query("user",new String[]{"username","password"},"username=?",new String[]{selection},null,null,null,null);
              Cursor cursor1=db.rawQuery("select userName,password from user where userName=?",new String[]{selection});
                showInfo.setText("查询结果如下");
while(cursor1.moveToNext())
{
showInfo.append("\n"+"用户名"+cursor1.getString(0)+",密码"+cursor1.getString(1));
}
cursor1.close();
db.close();
break;

}
}

//格式化快捷键 ctrl+alt+l
//数据库帮助类
class MyDbHelper extends SQLiteOpenHelper{
//构造器的作用,参数含义:上下文,数据库名称,结果集工厂,版本号
public MyDbHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
//数据库初始化,用来建表
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table user(user_id integer primary key autoincrement,userName varchar(10),password varchar(10))");
}
//升级方法
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
}
}
 

运行结果如下:

普通查询

 

 条件查询

 

posted @ 2023-03-07 20:31  伽澄  阅读(80)  评论(0编辑  收藏  举报