如何从sqlite数据库中的列中选择值,该列将以用户插入的特定字母开头

时间:2016-04-26 04:42:47

标签: android sqlite android-sqlite

我想用sqlite术语中的以下sql代码:

"SELECT * FROM babynames WHERE name like '$id%'"

我得到以下的错误 SQLiteLog:(1)near" like&#34 ;: syntax error

我的代码:

public class Datalist extends ListActivity{

public String id1;
RegistrationAdapter adapter_ob;
 DBhandler helper_ob;
 SQLiteDatabase db_ob;
 ListView nameList;
 Cursor cursor;
public ListView listView;

ArrayList<HashMap<String, String>> list = new ArrayList();
private ArrayList<String> results = new ArrayList<String>();
String tableName = DBhandler.TABLE_NAME;
String n=DBhandler.KEY_NAME;
String m=DBhandler.KEY_MEAN;
private SQLiteDatabase newDB;
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.list);

        id1 = getIntent().getStringExtra(config.TAG_ID);
        openAndQueryDatabase();
        displayResultList();


    }
private void displayResultList() {
    // TODO Auto-generated method stub
    //TextView tView = new TextView(this);
    //tView.setText("This data is retrieved from the database and only 4 " +
    //"of the results are displayed");
    //getListView().addHeaderView(tView);

    setListAdapter(new ArrayAdapter<String>(this,
    android.R.layout.simple_list_item_1, results));
    getListView().setTextFilterEnabled(true);

}
private void openAndQueryDatabase() {
    // TODO Auto-generated method stub
    try {
        DBhandler dbHelper = new DBhandler(this.getApplicationContext());
        newDB = dbHelper.getWritableDatabase();
        Cursor c = newDB.rawQuery("SELECT _name, _mean FROM " +
        tableName + "where _name like'" +id1+ "%'" , null);

        if (c != null ) {
        if (c.moveToFirst()) {
        do {
        //Integer id=c.getInt(c.getColumnIndex("_id"));
        String Name = c.getString(c.getColumnIndex("_name"));
        String mean= c.getString(c.getColumnIndex("_mean"));

        /* HashMap<String, String> baby = new HashMap();
        //baby.put(config.TAG_ID, id);
        baby.put(config.TAG_NAME, Name);
        baby.put(config.TAG_MEAN1, mean);
        list.add(baby);*/
        results.add("" + Name + ""
                + "" + mean);

        }while (c.moveToNext());
        }
        }
        } catch (SQLiteException se ) {
        Log.e(getClass().getSimpleName(), "Could not create or Open the database");
        } 
    finally {
        if (newDB != null)
        newDB.execSQL("DELETE FROM " + tableName);
        newDB.close();
        //listView.setAdapter(new SpecialAdapter(this, list, R.layout.list_item, new String[]{config.TAG_NAME, config.TAG_MEAN1}, new int[]{R.id.name, R.id.mean1}));
        }
     //listView.setAdapter(new SpecialAdapter(this, list, R.layout.list_item, new String[]{config.TAG_NAME, config.TAG_MEAN1}, new int[]{R.id.name, R.id.mean1}));

        }

}

任何人都请帮帮我。谢谢。

1 个答案:

答案 0 :(得分:3)

试试这个......

 public Cursor getBabyName(String query) {
        return db.query(DbHelper.NAME_TABLE, NAME_FIELDS,
                DbHelper.BABY_NAME+ " like '" + query + "%'", null,
                null, null, null, null);
    }