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

时间:2016-11-06 01:16:09

标签: java android database sqlite

有没有办法从创建的数据库中获取列,并存储 在列中找到的数值到数组?

我的主要目标是从列中获取值,并将它们存储到微调器中。

下面查看代码!

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) {

    }
}

上面是我的数据库处理程序类,我想获取列PATIENT_FNAME并将所有这些值存储到数组中。然后,在我的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();

    }    
}

1 个答案:

答案 0 :(得分:0)

您可以执行类似

的操作
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();
}