【问题标题】:How to get choice to query based on a column drop down value and get the sum of another column?如何根据列下拉值选择查询并获取另一列的总和?
【发布时间】:2014-12-18 23:36:39
【问题描述】:

你好,我刚学安卓我有一张桌子

rollno分类付款日期金额

1 租金现金 11/11/2014 10

2 实用卡 12/11/2014 20

3 运输现金 15/12/2014 10

我在表中使用类别列的微调器来选择数据类别,我希望用户能够根据他们选择的类别在此表中查询例如(租金、运输、实用程序...)如果用户选择租金,他们会看到它的所有数据,加上我希望查询总计租金类别金额列的总和。我希望我没有让任何人感到困惑我非常感谢您的指导谢谢。

目前只能选择如下 如果(查看==btnShowInfo) { 光标 c=db.rawQuery("SELECT * FROM fee WHERE Category ='Utility'", null);

她是我的密码:

  public class MainActivity extends Activity implements OnClickListener
{
    EditText editRollno,editName,editamount ,category,editDate,Category,Paidby,amount,expense;
    int Amount;
    Button btnAdd,btnDelete,btnModify,btnView,btnViewAll,btnShowInfo;
    Spinner mCategory;
    Spinner SpPayType;
    ArrayAdapter<CharSequence> adapter;
    SQLiteDatabase db;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editRollno=(EditText)findViewById(R.id.editRollno);

       // Spinners  
        mCategory = (Spinner)findViewById(R.id.spCategory);
        SpPayType   = (Spinner)findViewById(R.id.sp_PayType);

        adapter = ArrayAdapter.createFromResource(this, R.array.Expense_Category,
                android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
         mCategory.setAdapter(adapter);


        adapter = ArrayAdapter.createFromResource(this, R.array.Payment_Option, 
                android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        SpPayType.setAdapter(adapter);

        editDate    =(EditText)findViewById(R.id.editDate);
        editamount  =(EditText)findViewById(R.id.editAmount);
        btnAdd      =(Button)findViewById(R.id.btnAdd);
        btnDelete   =(Button)findViewById(R.id.btnDelete);
        btnModify   =(Button)findViewById(R.id.btnModify);
        btnView     =(Button)findViewById(R.id.btnView);
        btnViewAll  =(Button)findViewById(R.id.btnViewAll);
        btnShowInfo =(Button)findViewById(R.id.btnShowInfo);

        btnAdd.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        btnModify.setOnClickListener(this);
        btnView.setOnClickListener(this);
        btnViewAll.setOnClickListener(this);
        btnShowInfo.setOnClickListener(this);

        db=openOrCreateDatabase("HHECDB", Context.MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS expense(rollno INTEGER PRIMARY KEY ,Category VARCHAR, Paidby VARCHAR,date VARCHAR, amount VARCHAR);");
    }
    public void onClick(View view)

    {
        if(view==btnAdd)
        {
            if(editRollno.getText().toString().trim().length()==0||
               mCategory.getSelectedItem().toString().trim().length()==0||
               SpPayType.getSelectedItem().toString().trim().length()==0||
               editDate.getText().toString().trim().length()==0||
               editamount.getText().toString().trim().length()==0)
            {
                showMessage("Error", "Please enter all values");
                return;
            }
            db.execSQL("INSERT INTO expense VALUES('"+editRollno.getText()+"','"+mCategory.getSelectedItem()+
                       "','"+SpPayType.getSelectedItem()+"','"+editDate.getText()+"','"+editamount.getText()+"');");
            showMessage("Success", "Record added");
            clearText();
        }
        if(view==btnDelete)
        {
            if(editRollno.getText().toString().trim().length()==0)
            {
                showMessage("Error", "Please enter Rollno");
                return;
            }
            Cursor c=db.rawQuery("SELECT * FROM expense WHERE rollno='"+editRollno.getText()+"'", null);
            if(c.moveToFirst())
            {
                db.execSQL("DELETE FROM expense WHERE rollno='"+editRollno.getText()+"'");
                showMessage("Success", "Record Deleted");
            }
            else
            {
                showMessage("Error", "Invalid Rollno");
            }
            clearText();
        }
        if(view==btnModify)
        {
            if(editRollno.getText().toString().trim().length()==0)
            {
                showMessage("Error", "Please enter Rollno");
                return;
            }
            Cursor c=db.rawQuery("SELECT * FROM expense WHERE rollno='"+editRollno.getText()+"'", null);
            if(c.moveToFirst())
            {
            /*  db.execSQL("UPDATE student SET name='"+editName.getText()+"',marks='"+editMarks.getText()+
                        "' WHERE rollno='"+editRollno.getText()+"'"); */
                db.execSQL("UPDATE expense SET Category='"+mCategory.getSelectedItem()+"',paidby ='"+SpPayType.getSelectedItem()+"',date='"+editDate.getText()+
                        "',amount='"+editamount.getText()+
                        "' WHERE rollno='"+editRollno.getText()+"'");

                showMessage("Success", "Record Modified");
            }
            else
            {
                showMessage("Error", "Invalid Rollno");
            }
            clearText();
        }
        if(view==btnView)
        {
            if(editRollno.getText().toString().trim().length()==0)
            {
                showMessage("Error", "Please enter Rollno");
                return;
            }
            Cursor c=db.rawQuery("SELECT * FROM expense WHERE rollno='"+editRollno.getText()+"'", null);
            if(c.moveToFirst())
            {
                ///
                Category.setText(c.getString(1));
                Paidby.setText(c.getString(3));
                editDate.setText(c.getString(3));
                editamount.setText(c.getString(4));
            }
            else
            {
                showMessage("Error", "Invalid Rollno");
                clearText();
            }
        }
        if(view==btnViewAll)
        {

            Cursor c=db.rawQuery("SELECT * FROM expense", null);
            if(c.getCount()==0)
            {
                showMessage("Error", "No records found");
                return;
            }
            StringBuffer buffer=new StringBuffer();
            while(c.moveToNext())
            {
                buffer.append("Rollno: "+c.getString(0)+"\n");
                buffer.append("Category: "+c.getString(1)+"\n");
                buffer.append("Paid By : "+c.getString(2)+"\n");
                buffer.append("Date : "+c.getString(3)+"\n");
                buffer.append("Amount: "+c.getString(4)+"\n\n");
            }
            showMessage("Expense Details", buffer.toString());
        }/* this wher i want the user if showInfo btn pressed be able to choose all  category */

        if(view==btnShowInfo)
        {
            Cursor c=db.rawQuery("SELECT * FROM expense WHERE Category ='Utility' ", null);
            if(c.getCount()==0)
            {
                showMessage("Error", "No records found");
                return;
            }
            StringBuffer buffer=new StringBuffer();
            while(c.moveToNext())
            {
                buffer.append("Rollno: "+c.getString(0)+"\n");
                buffer.append("Category: "+c.getString(1)+"\n");
                buffer.append("Paid By : "+c.getString(2)+"\n");
                buffer.append("Date : "+c.getString(3)+"\n");
                buffer.append("Amount: "+c.getString(4)+"\n\n");
            }
            showMessage("Expense Details", buffer.toString());
        }}

    public void showMessage(String title,String message)
    {
        Builder builder=new Builder(this);
        builder.setCancelable(true);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.show();
    }
    public void clearText()
    {
        editRollno.setText("");
        mCategory.setSelected(true);
        SpPayType.setSelected(true);
        editDate.setText("");
        editamount.setText("");
        editRollno.requestFocus();
    }
}

【问题讨论】:

    标签: android sqlite


    【解决方案1】:

    试试这个查询

    选择卷号, 类别, SUM(金额) 从费用 其中类别 =“选定类别” GROUP BY 罗尔诺, 类别

    【讨论】:

      猜你喜欢
      • 2020-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多