Java Android。在DatabaseHelper中执行查询时出错

时间:2011-12-07 22:37:07

标签: java android string sqlite textview

我想从sqlite中检索所有与旋转器中选择的产品具有相同市场的产品

来自databasehelper的函数是:

public String getSelectedData(String market) {
    // TODO Auto-generated method stub
    String[] columns = new String[]{KEY_PRODNAME, KEY_PRODAMOUNT ,KEY_AMOUNTTYPE, KEY_MARKETID};
    Cursor c= myDatabase.query(DATABASE_PRODTABLE, columns, KEY_MARKETID + "=" + market, null, null, null, null);
    String result = "";

    //int iRow = c.getColumnIndex(KEY_PRODROWID);
    int iName = c.getColumnIndex(KEY_PRODNAME);
    int iAmount = c.getColumnIndex(KEY_PRODAMOUNT);
    int iAmountType = c.getColumnIndex(KEY_AMOUNTTYPE);
    int iMarket = c.getColumnIndex(KEY_MARKETID);
    int i=0;
    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        i++;
        result = result + i + ". " + c.getString(iName) + " " + c.getString(iAmount) + " " + c.getString(iAmountType) + " " +  c.getString(iMarket) +  "\n";
    }
    return result;

}

执行getSelectedData(market)时,它给出了一个错误

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setare continut din layout
    setContentView(R.layout.viewlist);
    TextView tv=(TextView)findViewById(R.id.tvSQLinfo);
    buton = (Button)findViewById(R.id.button1);
    buton.setOnClickListener(this);
    Bundle bundle = getIntent().getExtras();
    String market = bundle.getString("market");

    //tv.setText("Data send from previous intent nSite Name = +" + market );
    //Afisare produse din baza de date
   DatabaseHelper info= new DatabaseHelper(ViewProductsActivity.this);
    try {
        info.open();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
  String data = info.getSelectedData(market);
  tv.setText(data);
  info.close();

1 个答案:

答案 0 :(得分:0)

考虑将KEY_MARKETID声明为NUMERIC或INTEGER,建议您应始终使用数字作为ID而不是字符串。 这是一个例子:

private static final String BUDGETS_CREATE=
"create table budgets ("+KEY_ROWID+" integer primary key autoincrement,"
+KEY_LIST+" text not null, "+KEY_BDGT+" integer not null, "+KEY_SOLD+"numeric);";

这是一个可能对sqlite数据类型有帮助的引用: http://www.sqlite.org/datatype3.html

您可以执行查询的另一种方法是使用raw query语句。 并在字符串中输入您的查询。

String sqlwhere ="SELECT * FROM budgets;";
Cursor cur=mDb.rawQuery(sqlwhere, null);