首页 > 其他分享 >软件工程日报十一——安卓studio的数据查询

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

时间:2023-03-07 20:46:06浏览次数:37  
标签:null 安卓 db 软件工程 studio import android password id

上一篇博客实现了安卓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) {

}
}
}
 

运行结果如下:

普通查询

 

 条件查询

 

标签:null,安卓,db,软件工程,studio,import,android,password,id
From: https://www.cnblogs.com/jiacheng-712/p/17185553.html

相关文章

  • 如何在 OSX 上从命令行打开 Visual Studio Code?
    文档提到了一个名为的可执行文件,但我不确定在哪里可以找到它,所以我可以将它放在我的路径上。code我从VSCode站点下载的zip不包含任何此类可执行文件。(我能够运行......
  • 关于Android Studio的Activity的页面跳转完成
    第一种方式Intentintent=newIntent();intent.setClass(this,MainActivity3.class);startActivity(intent);第二种方式Intentintent=newIntent();intent.setClas......
  • 软件工程学习第十二天
    今天我们上课讲解了如何规范代码,规范代码十分重要。代码是需要维护的,无论是自己维护,还是其他人维护,都需要阅读代码。符合规范的代码,能减少理解成本。其次,代码规范最大的目......
  • 2023年3月6日(软件工程日报)
    今天完成个人作业第一部分内容,其中最长打卡天数未能实现,打卡显示没有完成,在第二阶段会加以完善。以下为个人代码java代码方面,包括闹钟设定,登录设定,注册设定,封装类,连接数......
  • 【Android Studio】通过编辑setting.gradle文件,添加阿里仓库
    本人对AndroidStudio的了解非常初级,这篇blog主要是自用备忘性质。因为众所周知的原因,国外仓库访问很不方便,影响项目构建。所以需要添加国内仓库,而阿里云仓库属于比较知名......
  • 每日总结2023/3/6(安卓连接mysql)
    更换网络需要更改ip如何查找自己ip?cmd-输入ipconfig  先上结果   原文链接(26条消息)mysql5.7.35安装配置教程【超级详细安装教程】_qq-1438608594的博客-......
  • 记录--uni-app中安卓包检查更新、新版本下载、下载进度条显示功能实现
    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助需求描述如果想要做一个app的话,可以有很多种选择方案,uni-app是其中的一个性价比高一些(坑多一些)的方案。......
  • 安卓NDK本地开发中使用logcat打印日志
    配置在需要打印日志的文件中添加以下头文件和宏定义#include<android/log.h>#defineLOGD(...)__android_log_print(ANDROID_LOG_INFO,"LOG_TAG",__VA_ARGS__)#de......
  • 2023.3.6软件工程日报
    所花时间:3小时 代码量:100行 博客量:1 今天由于课上验收加了0.5分日期为2023.3.6    此外看了其他优秀同学的作品,深感自己的差距,感觉应该更细化业务逻辑......
  • Unity开发时,在visual studio编辑器中没有中文注释的解决办法
    在做Unity开发的时候,会遇到很多标准库或.Net库里的一些函数都没有注释,但是用VS单独创建一个WinForms桌面类型的程序,里面调用的都是有中文注释的。下面是步骤:使用VS创建......