如何使用查询检索用户数据

时间:2014-07-18 08:56:03

标签: android

我有一个项目应用程序,用户可以向医生询问时间表。

我的问题是,我不知道如何根据所选医生检索患者数据,并将其显示在ListView的医生档案中,以便医生可以看到患者要求时间表他们登录。

数据库

public class SqlDbHelper extends SQLiteOpenHelper {

public static final String DATABASE_TABLE_DOC = "doc_table";
public static final String DATABASE_TABLE_PATIENT = "patient_table";
public static final String DATABASE_NAME = "doc_db";
public static final int DATABASE_VERSION = 1;

public static final String _id = "_id";
public static final String COL_REG_ID = "reg_id";
public static final String COL_DOC_CODE = "doc_code";
public static final String COL_FNAME = "firstname";
public static final String COL_LNAME = "lastname";
public static final String COL_SPEC = "specialty";
public static final String COL_CARD_ID = "card_id";
public static final String COL_REGION = "region";
public static final String COL_LOCATION = "location";
public static final String COL_CONTACT = "contact";
public static final String SCHED_MON = "monday";
public static final String SCHED_TUE = "tuesday";
public static final String SCHED_WED = "wednesday";
public static final String SCHED_THU = "thursday";
public static final String SCHED_FRI = "friday";
public static final String SCHED_SAT = "saturday";
public static final String SCHED_SUN = "sunday";
public static final String COL_PASS = "password";
public static final String COL_CONPASS = "confirm_password";
private static final String CREATE_DATABASE = "CREATE TABLE "
        + DATABASE_TABLE_DOC + " (" + _id
        + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_REG_ID
        + " TEXT NOT NULL, " + COL_DOC_CODE + " TEXT NOT NULL, "
        + COL_FNAME + " TEXT NOT NULL, " + COL_LNAME + " TEXT NOT NULL, "
        + COL_SPEC + " TEXT NOT NULL, " + COL_CARD_ID + " TEXT NOT NULL, "
        + COL_REGION + " TEXT NOT NULL, " + COL_LOCATION
        + " TEXT NOT NULL, " + COL_CONTACT + " TEXT NOT NULL, " + SCHED_MON
        + " TEXT NOT NULL, " + SCHED_TUE + " TEXT NOT NULL, " + SCHED_WED
        + " TEXT NOT NULL, " + SCHED_THU + " TEXT NOT NULL, " + SCHED_FRI
        + " TEXT NOT NULL, " + SCHED_SAT + " TEXT NOT NULL, " + SCHED_SUN
        + " TEXT NOT NULL, " + COL_PASS + " TEXT NOT NULL, " + COL_CONPASS
        + " TEXT NOT NULL);";

public static final String id = "_id";
public static final String PAT_ID = "patient_id";
public static final String PAT_CARD_ID = "patient_cardid";
public static final String PAT_FNAME = "patient_firstname";
public static final String PAT_MNAME = "patient_middlename";
public static final String PAT_LNAME = "patient_lastname";
public static final String PAT_ADDRESS = "patient_address";
public static final String PAT_AGE = "patient_age";
public static final String PAT_DOB = "patient_dob";
public static final String PAT_BTYPE = "patient_bloodtype";
public static final String PAT_MEDALLERGY = "patient_medallergy";
public static final String PAT_MEDCOND = "patient_medcondition";
public static final String PAT_CONTACT = "patient_contact";
private static final String CREATE_PAT_DATABASE = "CREATE TABLE "
        + DATABASE_TABLE_PATIENT + " (" + _id
        + " INTEGER PRIMARY KEY AUTOINCREMENT, " + PAT_ID
        + " TEXT NOT NULL, " + PAT_CARD_ID + " TEXT NOT NULL, " + PAT_FNAME
        + " TEXT NOT NULL, " + PAT_MNAME + " TEXT NOT NULL, " + PAT_LNAME
        + " TEXT NOT NULL, " + PAT_ADDRESS + " TEXT NOT NULL, " + PAT_AGE
        + " TEXT NOT NULL, " + PAT_DOB + " TEXT NOT NULL. " + PAT_BTYPE
        + " TEXT NOT NULL, " + PAT_MEDALLERGY + " TEXT NOT NULL, "
        + PAT_MEDCOND + " TEXT NOT NULL, " + PAT_CONTACT
        + " TEXT NOT NULL);";

public SqlDbHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL(CREATE_DATABASE);
    db.execSQL(CREATE_PAT_DATABASE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_DOC);
    db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_PATIENT);
    onCreate(db);
 }

}

2 个答案:

答案 0 :(得分:2)

String query = "SELECT * FROM " + TABLE NAME + " where "+ PAT_ID + "='" + user Id + "'";

SQLiteDatabase db = dbHelper.getHandle();

cursor = db.rawQuery(query, null);    
cursor.moveToLast();    
startManagingCursor(cursor);

if (cursor.moveToFirst()) {    
    do {    
        // do ....
        String id = cursor.getString(0);    
    } while (cursor.moveToNext());    
}

答案 1 :(得分:0)

在SqlDbHelper类中编写此方法:

public ArrayList<String> getPatientNames(String docId) {

    ArrayList<String> list = new ArrayList<String>();
    String query = "Select * from  DATABASE_TABLE_PATIENT where _id ='"
            + docId+ "'";
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query, null);

    if (cursor.moveToFirst()) {
        do {

            db.beginTransaction();
            try {
                list.add(cursor.getString(3)+" "+cursor.getString(4)+" "+cursor.getString(5));
                db.setTransactionSuccessful();
            } catch (SQLException e) {
                e.printStackTrace();
                db.endTransaction();
            } finally {
                db.endTransaction();
            }
        } while (cursor.moveToNext());
    }
    return list;
}

上述方法将返回与&#34; _id&#34;相对应的所有患者的姓名列表。医生通过了这种方法。

现在使用患者名的ArrayList为ListView制作适配器:

SqlDbHelper obj = new SqlDbHelper(this);
ArrayList<String> patients = obj.getPatientNames(docId)   //Pass the doctor id here

ArrayAdapter<String> adapter  = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, patients);

listView.setAdapter(adapter); // Set this adapter to your listview