简体   繁体   中英

Android SQLite get value from column

I have a column ShopName, and another column called LocationCode, if the LocationCode equals to "SKP", the corresponding ShopName will be added to an array, my question is, how can I do that?

I got something like this:

public List<String> getQuotes() {
    String WhiteFarm = "WhiteFarm";
    List<String> list = new ArrayList<>();
    Cursor cursor = database.rawQuery("SELECT * FROM WhereToEat WHERE 'LocationCode' = " + WhiteFarm, null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        list.add(cursor.getString(0));
        cursor.moveToNext();
    }
    cursor.close();
    return list;
}

And another class like this:

public class DatabaseGrabber extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.selection_main);

    final TextView Shop_name = (TextView)findViewById(R.id.ShopName);

    DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);
    databaseAccess.open();
    final List<String> ShopName = databaseAccess.getQuotes();
    databaseAccess.close();

    Button StartDraw = (Button)findViewById(R.id.draw_start);
    StartDraw.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Collections.shuffle(ShopName);
            String random = ShopName.get(0);
            Shop_name.setText(random);
        }
    });
}
}

如果您的 LocationCode 包含 String(或 SQLite 的 TEXT)类型的数据,则您的值应括在单引号中,如下面的代码所示:

Cursor cursor = database.rawQuery("SELECT * FROM WhereToEat WHERE LocationCode = " + "'" + WhiteFarm "'", null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM