如何用变量查询我的sqlite数据库

时间:2013-12-17 21:26:18

标签: java android sqlite filter

我想在textview中显示我选择的月份。要做到这一点,我有一个微调器,当我在微调器中选择一个月时,这应该找到我在这个特定月份的所有数据。 我不确定如何使用变量过滤查询,或者如何将变量从我的Start.java类传递到我的KilometerSQL.java类。

由于我没有使用意图转到Kilometer.java类,我不知道如何传递变量。

这是我的Start.java类

@Override
public void onItemSelected(AdapterView<?> parent, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub
    String month = parent.getSelectedItem().toString(); //This is my month variable

    KilometerSQL spinner = new KilometerSQL(this);
    spinner.open();

    String dataSpinner = KilometerSQL.spinnerData();
    spinner.close();

    logbog.setText(dataSpinner);

}

这是我的KilometerSQL.java类

public static String spinnerData() {
    // TODO Auto-generated method stub
    String [] columns = new String []{ KEY_DAY, KEY_MONTH, KEY_YEAR, KEY_KILOMETER, KEY_LOCATIONS};
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_MONTH, null, null, null, null);
    String result = "";

    int iDay = c.getColumnIndex(KEY_DAY);
    int iMonth = c.getColumnIndex(KEY_MONTH);
    int iYear = c.getColumnIndex(KEY_YEAR);
    int iKilometer = c.getColumnIndex(KEY_KILOMETER);
    int iLocations = c.getColumnIndex(KEY_LOCATIONS);

    for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){
        result = result + c.getString(iDay) + "-" + c.getString(iMonth) + "-" + c.getString(iYear) + " " + c.getString(iKilometer) + " " + c.getString(iLocations) + "\n";
    }

    return result;
}

我没有运气使用与其他用户类似的答案。

感谢所有帮助。

1 个答案:

答案 0 :(得分:1)

在您的Start.java类中:

Intent i = new Intent(this, KilometerSQL.class);
i.putExtra("KEY",YourData);

在KilometerSQL.java类中

Bundle extras = getIntent().getExtras();
if(extras !=null) {
    String value = extras.getString("KEY");
}

/ /Declare ur Textview here and set text to value

修改 您是否在上面尝试过,或者您也可以在开始课中尝试spinnerData(dataSpinner)并在killometerSql类中更改String spinnerData(String dataSpinner)

启动课程

public void onItemSelected(AdapterView<?> parent, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub
    String month = parent.getSelectedItem().toString(); //This is my month variable

    KilometerSQL spinner = new KilometerSQL(this);
    spinner.open();

    String dataSpinner = KilometerSQL.spinnerData();//you said u want to pass from start to killometer ..Whats this line doing though
    spinner.close();

    logbog.setText(dataSpinner);
    spinnerData(month);


}

Killometer class

 public static String spinnerData(String KEY_MONTH) {
        // TODO Auto-generated method stub
        String [] columns = new String []{ KEY_DAY, KEY_MONTH, KEY_YEAR, KEY_KILOMETER, KEY_LOCATIONS};
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_MONTH, null, null, null, null);
        String result = "";

        int iDay = c.getColumnIndex(KEY_DAY);
        int iMonth = c.getColumnIndex(KEY_MONTH);
        int iYear = c.getColumnIndex(KEY_YEAR);
        int iKilometer = c.getColumnIndex(KEY_KILOMETER);
        int iLocations = c.getColumnIndex(KEY_LOCATIONS);

        for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){
            result = result + c.getString(iDay) + "-" + c.getString(iMonth) + "-" + c.getString(iYear) + " " + c.getString(iKilometer) + " " + c.getString(iLocations) + "\n";
        }

        return result;
    }