Android SQLITE!从同一表查询多个数据(列)

时间:2019-02-25 17:50:46

标签: java android android-sqlite

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");
}

}

我正在尝试从该数据库的表中查询多个数据(列)。我该怎么办?我相信它与我的getLatestPrice()函数中查询语句的格式有关

希望我能得到一些帮助。非常感谢

1 个答案:

答案 0 :(得分:0)

对于初学者,请在您的 getLatestPrice 部分尝试类似的操作

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

这将为您提供您所需的光标!包含您想要的表中的所有数据! 并且如果您想获取任何特定列和其他内容,那么您必须首先这样做

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;    
    }

有关此link here

的详细信息和设置,为了更容易

有关适用于Android的此部分的更多高级信息和Google文档,请参见! link here