简体   繁体   English

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

[英]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() 我该怎么办?我相信它与我的getLatestPrice()函数中查询语句的格式有关。

Hope I could get some help.Much appreciated 希望我能得到一些帮助。

for a start brother try something like this in the getLatestPrice part of yours 对于新手兄弟,请在您的getLatestPrice部分尝试类似的操作

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! 有关适用于Android的此部分的更多高级信息和Google文档,请参见! link here 链接到这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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