2021/02/9

进行数据的查找

package com.example.bookkeeping;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

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

public class chazhao2 extends AppCompatActivity {
    private MyDatabaseHelper dbHelper;
    String str="";

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chaozhao2);

        Bundle bundle = getIntent().getExtras();
       String name= bundle.getString("name");

        Log.d("chazhao2","name"+name);
        final TextView tv=(TextView)findViewById(R.id.mm);
        dbHelper=new MyDatabaseHelper(this,"zhangben.db",null,4);
        SQLiteDatabase db=dbHelper.getWritableDatabase();
         Cursor cursor=db.query("zhangben",null,"时间=?",new String[]{name},
        null,null,null);//读取数据库里面专业是软件工程的所有信息
        if(cursor.moveToFirst()){
            do{
                String ye=cursor.getString(cursor.getColumnIndex("盈亏"));
                String why=cursor.getString(cursor.getColumnIndex("原因"));
                String money=cursor.getString(cursor.getColumnIndex("金额"));
                String time=cursor.getString(cursor.getColumnIndex("时间"));
                Log.d("chazhao2","盈亏为:"+ye);
                Log.d("chazhao2","原因为:"+why);
                Log.d("chazhao2","金额为:"+money);
                Log.d("chazhao2","时间为:"+time);
                str+=ye+"   "+ "    "+why+"     金额:"+money+""+time+"
";//将数据库信息存到str中并换行
            }while (cursor.moveToNext());
        }
        cursor.close();
        tv.setText(str);//打印信息
    }
}
原文地址:https://www.cnblogs.com/qiangini/p/14905038.html