简体   繁体   English

SQLiteDatabase:获取列并存储到数组中

[英]SQLiteDatabase: Get column and store into an array

Is there a way to grab a column from the database created, and store the values found in the column into an array? 有没有办法从创建的数据库中获取列,并将列中找到的值存储到数组中?

My main goal, is to get values in from a column, and store them into a spinner. 我的主要目标是从列中获取值,并将它们存储到微调器中。


Look below for code! 看下面的代码吧!

PatientDbHelper.java PatientDbHelper.java

package tanav.sharma;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Tanav on 11/4/2016.
 */
public class PatientDbHelper extends SQLiteOpenHelper {


    private static final String DATABASE_NAME = "PATIENTINFO.db";
    private static final int DATABASE_VERSION = 1;

    private static final String CREATE_QUERY = "CREATE TABLE "
            + PatientInfo.NewPatientInfo.TABLE_NAME + " ("
            + PatientInfo.NewPatientInfo.PATIENT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + PatientInfo.NewPatientInfo.PATIENT_FNAME+ " TEXT,"
            + PatientInfo.NewPatientInfo.PATIENT_LNAME+" TEXT,"
            + PatientInfo.NewPatientInfo.PATIENT_DEPARTMENT+" TEXT);";


    private static final String CREATE_QUERY_TEST =
            "CREATE TABLE "
                    + TestInfo.NewTestInfo.TABLE_TEST_NAME + " ("
                    + TestInfo.NewTestInfo.TEST_BlOOD_PRESSURE + " TEXT,"
                    + TestInfo.NewTestInfo.TEST_BLOOD_OXYGEN_LEVEL + " TEXT,"
                    + TestInfo.NewTestInfo.TEST_RESPITORY_RATE +" TEXT,"
                    + TestInfo.NewTestInfo.TEST_HEART_RATE +" TEXT);";

    public PatientDbHelper(Context context){

        super(context,DATABASE_NAME,null,DATABASE_VERSION);
        Log.e("DATABASE OPERATIONS","Database created / opened ...");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_QUERY);
        db.execSQL(CREATE_QUERY_TEST);
        Log.e("DATABASE OPERATIONS","Table created...");

    }

    public void addInformations(int id, String fname, String lname, String department, SQLiteDatabase db){

        ContentValues contentValues = new ContentValues();
        if ( id != 0 ){ contentValues.put(PatientInfo.NewPatientInfo.PATIENT_ID, id); }
        contentValues.put(PatientInfo.NewPatientInfo.PATIENT_FNAME,fname);
        contentValues.put(PatientInfo.NewPatientInfo.PATIENT_LNAME,lname);
        contentValues.put(PatientInfo.NewPatientInfo.PATIENT_DEPARTMENT,department);


        db.insert(PatientInfo.NewPatientInfo.TABLE_NAME, null,contentValues);

        Log.e("DATABASE OPERATIONS","One in row inserted...");
    }

    public void addTestInformation(/*String BP*/ int BL, int RR, int HR, SQLiteDatabase db){

        ContentValues contentTestValues = new ContentValues();
        //contentTestValues.put(TestInfo.NewTestInfo.TEST_BlOOD_PRESSURE, BP);
        contentTestValues.put(TestInfo.NewTestInfo.TEST_BLOOD_OXYGEN_LEVEL,BL);
        contentTestValues.put(TestInfo.NewTestInfo.TEST_RESPITORY_RATE, RR);
        contentTestValues.put(TestInfo.NewTestInfo.TEST_HEART_RATE, HR);


        db.insert(TestInfo.NewTestInfo.TABLE_TEST_NAME, null, contentTestValues);

        Log.e("DATABASE OPERATIONS", "One in row inserted...");
    }

   /* public Cursor getPatientDepartment(String departments, SQLiteDatabase sqLiteDatabase){
        String[] projections = {PatientInfo.NewPatientInfo.PATIENT_FNAME, PatientInfo.NewPatientInfo.PATIENT_LNAME, PatientInfo.NewPatientInfo.PATIENT_DEPARTMENT};
        String selection = PatientInfo.NewPatientInfo.PATIENT_DEPARTMENT+ " LIKE ?";
        String[] selection_args = {departments};

        Cursor cursor = sqLiteDatabase.query(PatientInfo.NewPatientInfo.TABLE_NAME,projections,selection,selection_args,null,null,null);

        return cursor;

    }*/


    public Cursor getPatientsId(int patient_id,SQLiteDatabase sqLiteDatabase){

        String[] projections = {PatientInfo.NewPatientInfo.PATIENT_ID, PatientInfo.NewPatientInfo.PATIENT_FNAME, PatientInfo.NewPatientInfo.PATIENT_DEPARTMENT};
        String selection = PatientInfo.NewPatientInfo.PATIENT_ID+ " LIKE ?";
        String convert = String.valueOf(patient_id);
        String[] selection_args = {convert};

        Cursor cursor = sqLiteDatabase.query(PatientInfo.NewPatientInfo.TABLE_NAME,projections,selection,selection_args,null,null,null);
        return cursor;

    }

    public Cursor getInformations(SQLiteDatabase db){
        Cursor cursor;
        String[] projections = {PatientInfo.NewPatientInfo.PATIENT_FNAME, PatientInfo.NewPatientInfo.PATIENT_LNAME, PatientInfo.NewPatientInfo.PATIENT_DEPARTMENT};
        cursor = db.query(PatientInfo.NewPatientInfo.TABLE_NAME,projections,null,null,null,null,null);
        return cursor;
    }

    public Cursor getTestInformation(SQLiteDatabase db){
        Cursor cursorTest;
        String[] testProjections = {TestInfo.NewTestInfo.TEST_HEART_RATE, TestInfo.NewTestInfo.TEST_RESPITORY_RATE, TestInfo.NewTestInfo.TEST_BLOOD_OXYGEN_LEVEL, TestInfo.NewTestInfo.TEST_BlOOD_PRESSURE};
        cursorTest = db.query(TestInfo.NewTestInfo.TABLE_TEST_NAME,testProjections,null,null,null,null,null);
        return cursorTest;
    }



    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

Above is my Database handler class, I want to grab the column PATIENT_FNAME and store all those values into an Array. 上面是我的数据库处理程序类,我想获取PATIENT_FNAME列并将所有这些值存储到一个数组中。 Then Later on, in my AddTest.java file, i want to display these values into an spinner. 然后,在我的AddTest.java文件中,我想将这些值显示到微调器中。

Below is my AddTest.java 下面是我的AddTest.java

package tanav.sharma;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class AddTest extends AppCompatActivity {

    private RadioGroup radioGroup;
    private RadioButton radioButton;

    EditText etBL, etRR, etHBR;
    Spinner patients;
    Context context;
    PatientDbHelper patientDbHelper;

    SQLiteDatabase sqLiteDatabase;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_test);

        getSupportActionBar().setTitle(R.string.add_test);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);



        etBL = (EditText) findViewById(R.id.etBOL);
        etRR = (EditText) findViewById(R.id.etRR);
        etHBR = (EditText) findViewById(R.id.etHBR);

        patients = (Spinner) findViewById(R.id.spinner);



    }

    public void addTest(View view){

        radioGroup = (RadioGroup) findViewById(R.id.radio);
        Button addTest = (Button) findViewById(R.id.addTest);

        addTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int selectedId = radioGroup.getCheckedRadioButtonId();
                RadioButton radioButton = (RadioButton) findViewById(selectedId);
            }
        });


        //String BP = radioButton.getText().toString();
        int BL = Integer.parseInt(etBL.getText().toString());
        int RR = Integer.parseInt(etRR.getText().toString());
        int HR = Integer.parseInt(etHBR.getText().toString());

        patientDbHelper = new PatientDbHelper(this);
        sqLiteDatabase = patientDbHelper.getWritableDatabase();
        patientDbHelper.addTestInformation(BL,RR,HR,sqLiteDatabase);
        Toast.makeText(getBaseContext(),"Data Saved",Toast.LENGTH_LONG).show();
        patientDbHelper.close();

    }    
}

You can do something like 你可以做点什么

List<String> names = new ArrayList<>();
Cursor cursor = patientDbHelper.getInformations(sqLiteDatabase);
if (cursor != null){
    while(cursor.moveToNext()){
        names.add(cursor.getString(cursor.getColumnIndex(PatientInfo.NewPatientInfo.PATIENT_FNAME)));
    }
    cursor.close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM