简体   繁体   中英

Android SQLITE! Query more than one data(columns) from the same table

public class DatabaseHelper4 extends SQLiteOpenHelper {
private final static String DBNAME = "Bidprice";
private final static int DBVERSION = 2;

SQLiteDatabase mDB2;

public final static String TBL_BID = "bidprice";
public final static String COL_BID_ID = BaseColumns._ID;
public final static String COL_BID_PRICE = "pricebid";
public final static String COL_BID_NAME = "namebid";


private String crt_tbl_bidprice = "CREATE TABLE IF NOT EXISTS " + TBL_BID + "(" +
        COL_BID_ID + " INTEGER PRIMARY KEY, " +
        COL_BID_PRICE + " TEXT, " +
        COL_BID_NAME + " TEXT " +
        ")";

public DatabaseHelper4(Context context) {
    super(context, DBNAME, null, DBVERSION);
    mDB2 = this.getWritableDatabase();
}


@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(crt_tbl_bidprice);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

public long addPrice(String price,String nameprice) {
    ContentValues cv = new ContentValues();
    cv.put(COL_BID_PRICE,price);
    cv.put(COL_BID_NAME,nameprice);
    return mDB2.insert(TBL_BID,null,cv);
}



public Cursor getLatestPrice() {

    return mDB2.query(TBL_BID,null,null,null,null,null,COL_BID_PRICE + " DESC","1");
}

}

I am trying to query more than one data(column) from the table of this database. How do I go about it?I believe it has to do with the formatting of the query statement in my function of getLatestPrice()

Hope I could get some help.Much appreciated

for a start brother try something like this in the getLatestPrice part of yours

public Cursor getLatestPrice() {
  SQLiteDatabase db = this.getReadableDatabase();
  Cursor res =  db.rawQuery( "select * from "+TBL_BID, null);
  return res;    
}

This shall give you the cursor you require for now! with all the data in it from the table you want! and if you want to get any specific column/s and other stuff then you have to do like this for a start

public Cursor getLatestPrice() {
      SQLiteDatabase db = this.getReadableDatabase();

String[] projection = {
    COL_BID_PRICE,
    COL_BID_NAME
    };

Cursor res = db.query(
    TBL_BID,   // The table to query
    projection,             // The array of columns to return (pass null to get all)
    null,              // The columns for the WHERE clause
    null,          // The values for the WHERE clause
    null,                   // don't group the rows
    null,                   // don't filter by row groups
    null               // The sort order
    );

      return res;    
    }

For easier detail and setup reffer to this link here

For more advanced and google documentation on this part for android see! link here

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