字符串SQLquery无法从SQLite数据库中检索数据

时间:2014-04-19 13:32:29

标签: android sql sqlite

我试图从SQL数据库中检索数据。 在检索数据之前,用户必须在微调器中选择一个名称。 这里有微调值:

String name[] = {"Sandking","Bristleback","Sven","Tiny","Undying", "Naix","Weaver","Spectre","Lich"};   

接下来,我想使用spinner中的选定名称作为SQLquery的前置条件: 我把选中的名字,使用这个方法:

String nameSelected = spinner1.getSelectedItem().toString();

最后,我尝试将该字符串(" nameSelected")放在我的SQLquery上

public List<String> getAllRecord(String nameSelected) { 

    List<String> namagambar = new ArrayList<String>(); 
    String selectQuery = "SELECT * FROM " + TABLE_RECORD + " WHERE "
    + HERO_NAME +"="+nameSelected; 

    database = dbHelper.getReadableDatabase(); 
    cursor = database.rawQuery(selectQuery, null); 

    if (cursor.moveToFirst()) { 

        do { 

            namagambar.add(cursor.getString(5)); 
            } while (cursor.moveToNext()); 

    } 
    database.close(); 
    return namagambar; 

}

但它没有显示数据库中的任何数据。 看起来我犯了一个字符串nameSelected的错误,是否有人可以告诉我的错误以及如何解决它?

这是我的活动:

    public class help_activity extends Activity implements OnClickListener{

Spinner spinner1;
SQLiteConnector sqlConnect;
ListView lvUsers;
Button b1;



String colors[] = {"Sandking","Bristleback","Sven","Tiny","Undying", "Naix","Weaver","Spectre","Lich"}; 
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_help);


    lvUsers = (ListView) findViewById(R.id.listView1);
    b1 = (Button) findViewById(R.id.btn1);
    sqlConnect = new SQLiteConnector(this);
    addListenerOnSpinnerItemSelection();



    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            this, android.R.layout.simple_list_item_1, sqlConnect.getAllRecord(null));

    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            String nameSelected = spinner1.getSelectedItem().toString();
            if (nameSelected.equals("Sandking")) {
                lvUsers.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }
            // TODO Auto-generated method stub

        }
    });





}
public void addListenerOnSpinnerItemSelection() {
        spinner1 = (Spinner) findViewById(R.id.spinner1);

        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,colors );
        spinner1.setAdapter(spinnerArrayAdapter);
      }



@Override
public void onClick(View v) {


    /*if (v == b1) {


        ArrayAdapter<String> adapter = new ArrayAdapter<String>(

                this, android.R.layout.simple_list_item_1, sqlConnect.getAllRecord()); 

        lvUsers.setAdapter(adapter);
    */
}

}

1 个答案:

答案 0 :(得分:3)

字符串参数必须单引号 无论如何,你可以利用Android的参数binging并让它为你做到这一点。

更改此

String selectQuery = "SELECT * FROM " + TABLE_RECORD + " WHERE "
+ HERO_NAME +"="+nameSelected;

database = dbHelper.getReadableDatabase();
cursor = database.rawQuery(selectQuery, null);

String selectQuery = "SELECT * FROM " + TABLE_RECORD + " WHERE " + 
    HERO_NAME +" = ?";

database = dbHelper.getReadableDatabase();
cursor = database.rawQuery(selectQuery, new String[]{nameSelected});