【问题标题】:ANDROID/SQLite Search a recordANDROID/SQLite 搜索记录
【发布时间】:2013-02-15 22:01:34
【问题描述】:

我正在开发一个症状检查器应用程序。我的应用程序有一个带有复选框的listview(来自android.R.layout.simple_list_item_multiple_choice)。

检查以下症状并单击完成按钮后,应用程序应显示用户可能遇到的情况。信息已经存储在我预先填充的SQLite 数据库中。有关您的信息,请参阅此屏幕截图:

这是我目前所拥有的:(Symptoms.java)

       public void onClick(View v) {
    // TODO Auto-generated method stub

    /*Intent i = new Intent(this, SymptomsResult.class);
    startActivity(i);
    finish();*/

    dbHelper = new DBHelper(this);
      dbHelper.openDataBase();

      String c = dbHelper.getData();

    String selected = "";
    int cntChoice = lv.getCount();

    SparseBooleanArray sparseBooleanArray = lv.getCheckedItemPositions();

    for(int i = 0; i < cntChoice; i++){

        if(sparseBooleanArray.get(i)) {
            selected += lv.getItemAtPosition(i).toString() + "\n";
        }

    }

    c.equals(selected);

    dbHelper.close();


    Toast.makeText(this, selected, Toast.LENGTH_LONG).show();       

}

DBHelper.java

     public class DBHelper extends SQLiteOpenHelper{

//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/com.fps.myheartfirst/databases/";
private static String DB_NAME = "mhfDB";
private static String KEY_CONDITIONS = "conditions";
private static String KEY_SYMPTOMS = "symptoms";
private static String DB_TABLE = "tblSymptoms";
private SQLiteDatabase myDataBase; 
private final Context myContext;

/**
 * Constructor
 * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
 * @param context
 */

public DBHelper(Context context) {
    super(context, DB_NAME, null, 1);
    this.myContext = context;
}   

/**
 * Creates a empty database on the system and rewrites it with your own database.
 * */

public void createDataBase() throws IOException{

    boolean dbExist = checkDataBase();

    myDataBase = null;

     if (dbExist) {
            // do nothing - database already exist
        } else {

            // By calling this method and empty database will be created into
            // the default system path

            myDataBase = this.getReadableDatabase(); 
            myDataBase.close();


        try {
            copyDataBase(); 
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }
}

/**
 * Check if the database already exist to avoid re-copying the file each time you open the application.
 * @return true if it exists, false if it doesn't
 */

private boolean checkDataBase(){
    SQLiteDatabase checkDB = null;
    try{
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    }catch(SQLiteException e){
        //database doesn't exist yet.
    }
    if(checkDB != null){
        checkDB.close();
    }

    return checkDB != null ? true : false;
}

/**
 * Copies your database from your local assets-folder to the just created empty database in the
 * system folder, from where it can be accessed and handled.
 * This is done by transfering bytestream.
 * */

private void copyDataBase() throws IOException{

    //Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DB_NAME);

    // Path to the just created empty db
    String outFileName = DB_PATH + DB_NAME;

    //Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    //transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){
        myOutput.write(buffer, 0, length);
    }

    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();

}

public void openDataBase() throws SQLException{
    //Open the database
    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
}

@Override
public synchronized void close() {
    if(myDataBase != null)
        myDataBase.close();
    super.close();
}

@Override
public void onCreate(SQLiteDatabase db) {
}

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

public String getData() {

    String search = "";

    Cursor c = myDataBase.rawQuery("SELECT " + KEY_CONDITIONS + " FROM " + DB_TABLE + 
            " WHERE " + KEY_SYMPTOMS + " = '" 
    + search + "'", null);
            //new String[] { name }, null, null, null);

    int iRow = c.getColumnIndex(KEY_CONDITIONS);

    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        search = search + c.getString(iRow);
    }

    return search;

}

我的问题是,如何为我的症状类放置字符串查询?我对数据库编程有点陌生,所以请原谅。

我相信在这行我必须说点什么:

   c.equals(selected);

    dbHelper.close();


    Toast.makeText(this, selected, Toast.LENGTH_LONG).show();

因此将向用户显示 toast 以查看可能的条件。这里有人知道如何做到这一点吗?我会很感激你的帮助。谢谢。

【问题讨论】:

    标签: android sqlite checkbox


    【解决方案1】:
    public String getData( String search) {
    
     Cursor c = myDataBase.query("SELECT " + KEY_CONDITIONS + " FROM " + DB_TABLE + 
            " WHERE " + KEY_SYMPTOMS + " = '" 
    + search + "'", null);
    
    Cursor c = myDataBase.query(DB_TABLE ,KEY_SYMPTOMS+ " = ?",new String[]{search},null,null,null);
    
    
    int iRow = c.getColumnIndex(KEY_CONDITIONS);
    
    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        search = search + c.getString(iRow);
    }
    
    return search;
    
    }
    

    将搜索词传递给getData()方法

    【讨论】:

    • 为什么我需要使用另一个光标?你可以解释吗?谢谢
    • 你缺少一个论据。
    • 您好,我真的需要帮助。我按照你说的做了,但没有显示结果。
    • 只需替换您的 DBHelper 类中的 getData() 并替换 onClick 代码块 public void onClick(View v) { dbHelper = new DBHelper(this); dbHelper.openDataBase();选择的字符串 = ""; int cntChoice = lv.getCount(); SparseBooleanArray sparseBooleanArray = lv.getCheckedItemPositions(); for(int i = 0; i
    【解决方案2】:

    如下所示更改 getData 方法 -

    public String getData(String [] symptoms) {
        String search = null;
        Cursor c = myDataBase.query(DB_TABLE, new String [] {KEY_CONDITIONS}, KEY_SYMPTOMS + "= ? ", symptoms, null, null, null);
    
        c.moveToFirst();
    
        while (c.isAfterLast() == false) {
            search = "" + c.getString(0);
            c.moveToNext();
        }
    
        return search;
    
    }
    

    Symptoms 类中 - 将 onClick 更改为 -

    public void onClick(View v) {
    
        ArrayList<String> alSymptoms = new ArrayList<String>();
    
    
        dbHelper = new DBHelper(this);
    
        try {
    
            dbHelper.createDataBase();
    
        } catch (IOException ioe) {
    
            throw new Error("Unable to create database");
    
        }
    
        try {
    
            dbHelper.openDataBase();
    
        } catch (SQLException sqle) {
    
            throw sqle;
    
        }
    
    
    
        String selected = "";
    
        int cntChoice = lv.getCount();
    
        SparseBooleanArray sparseBooleanArray = lv.getCheckedItemPositions();
    
        for (int i = 0; i < cntChoice; i++) {
    
            if (sparseBooleanArray.get(i)) {
    
                //Log.i("Selected", lv.getItemAtPosition(i).toString());
                //selected += lv.getItemAtPosition(i).toString() + "\n";
                alSymptoms.add(lv.getItemAtPosition(i).toString());
            }
    
        }
        String [] symptoms = alSymptoms.toArray(new String[alSymptoms.size()]);
    
        String c = dbHelper.getData(symptoms);
    
        Toast.makeText(this, c, Toast.LENGTH_LONG).show();
    
        dbHelper.close();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多