如何根据条件在sqlite中选择所有语句?

时间:2014-08-12 09:17:41

标签: android mysql sqlite

我所拥有的是sqlite帮助程序类,我有两个这样的表:

public class InfoServicesNew extends SQLiteOpenHelper{
    //master
    private String KEY_ID = "ID";
    private String KEY_CUSTOMER_ID = "CUSTOMERID";
    private String KEY_CUSTOMER_NAME = "CUSTOMER";
    private String KEY_DATE = "DATE";

    //details
    private String KEY_ID_DETAILS = "DETAILSID";
    private String KEY_ITEM_ID = "ITEMID";
    private String KEY_ITEM_QUANTITY = "ITEM";
    private String KEY_EXTRA = "EXTRA";



    private static String DB_NAME = "ALIBABA";
    public static String DB_TABLE_NAME = "MASTER";
    public static String DB_TABLE_NAME2 = "DETAIL";
    private static int DB_VERSION = 3;

    private static SQLiteDatabase database;
    public InfoServicesNew(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        database = getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + DB_TABLE_NAME + " (" + KEY_ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT , " + KEY_CUSTOMER_ID +" TEXT,"  +
                KEY_CUSTOMER_NAME+" TEXT ,"+ KEY_DATE +" TEXT );");
        db.execSQL("CREATE TABLE " + DB_TABLE_NAME2 + " (" + KEY_ID_DETAILS
                + " INTEGER PRIMARY KEY AUTOINCREMENT , " + KEY_CUSTOMER_ID +" TEXT,"  +
                 KEY_ITEM_ID + " TEXT ," + KEY_ITEM_QUANTITY + " TEXT ,"+ KEY_EXTRA +" TEXT);");
    }

我已经正确地将它们插入到它们中,但事实是我想要制作一个像这样的选择函数:

从DETAIL中选择*,其中MASTER.KEY_CUSTOMER_ID == DETAIL.KEY_CUSTOMER_ID;

这是我的全部选择功能,如何编辑它以便我可以检索另一个表中具有相同客户ID值的所有行?

这是我建议的选择功能:

public ArrayList<cartItems> selectAllDetails(String Db_name) {
        String columns[] = new String[] { KEY_ID_DETAILS, KEY_CUSTOMER_ID,KEY_ITEM_ID,KEY_ITEM_QUANTITY,KEY_EXTRA};
        Cursor c = database.query(Db_name, columns, null, null,null,null,null);

        int idIndex = c.getColumnIndex(KEY_ID_DETAILS);
        int customerId = c.getColumnIndex(KEY_CUSTOMER_ID);
        int itemId = c.getColumnIndex(KEY_ITEM_ID);
        int ItemQuantity = c.getColumnIndex(KEY_ITEM_QUANTITY);
        int extra = c.getColumnIndex(KEY_EXTRA);
        ArrayList<cartItems> list = new ArrayList<cartItems>();
        if (c.moveToFirst()) {
            do {
                list.add(new cartItems(c.getInt(idIndex), c.getString(customerId), c.getString(itemId), c.getString(ItemQuantity),c.getString(extra)));
            } while (c.moveToNext());
        }
        return list;

    }  

请有人能给我一个简单的解决方案,因为我是sqlite的新手?谢谢

1 个答案:

答案 0 :(得分:2)

试试这个

    String sql="Select * from DETAIL inner join MASTER on DETAIL.KEY_CUSTOMER_ID = MASTER.KEY_CUSTOMER_ID";

     Cursor c=database.rawQuery(sql, null);

    int idIndex = c.getColumnIndex(KEY_ID_DETAILS);
    int customerId = c.getColumnIndex(KEY_CUSTOMER_ID);
    int itemId = c.getColumnIndex(KEY_ITEM_ID);
    int ItemQuantity = c.getColumnIndex(KEY_ITEM_QUANTITY);
    int extra = c.getColumnIndex(KEY_EXTRA);
    ArrayList<cartItems> list = new ArrayList<cartItems>();
    if (c.moveToFirst()) {
        do {
            list.add(new cartItems(c.getInt(idIndex), c.getString(customerId), c.getString(itemId), c.getString(ItemQuantity),c.getString(extra)));
        } while (c.moveToNext());
    }
    return list;